I solved the puzzle but I had a piece of code that wasn't working. I ended up using an if-else statement butI wanted to use a ternary operator. But it wasn't working saying that (number % 2 == 0) is not a statement.
for (int i = 0; i < intArray.size(); i++)
{
int number = intArray.get(i);
if (number % 2 == 0)
{
evenCount++;
}
else
{
oddCount++;
}
// (number % 2 == 0) ? evenCount++ : oddCount++; // doesn't work for some reason;
}
is there something I'm missing? Thanks in advance.package com.codegym.task.task06.task0606;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
/*
Even and odd digits
*/
public class Solution {
public static int even;
public static int odd;
public static void main(String[] args) throws IOException {
//write your code here
//initialize reader
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// read string from reader
String string = reader.readLine();
// split the string and store in String array
String[] stringArray = string.split("");
// initialize in int array
ArrayList<Integer> intArray = new ArrayList<Integer>();
// int[] intArray = new int[stringArray.length];
// change all characters to int and store in int array
for (int i = 0; i < stringArray.length; i++)
{
int integer = Integer.parseInt(stringArray[i]);
intArray.add(integer);
}
// initialize odd digits and even digits
int oddCount = 0;
int evenCount = 0;
// loop through intArray and compare to evenOddConditional
for (int i = 0; i < intArray.size(); i++)
{
int number = intArray.get(i);
if (number % 2 == 0)
{
evenCount++;
}
else
{
oddCount++;
}
// (number % 2 == 0) ? evenCount++ : oddCount++;
}
//print out results
System.out.println("Even: " + evenCount + " Odd: " + oddCount);
}
}