I need to find the sum of all odd digits of user input numeric string. For example, if the input is 12235, the sum would be 1 + 3 + 5 = 9.
This is what my code looks like so far:
import javax.swing.JOptionPane;
public class sumOfAllOddDigits
{
public static void main( String[]args )
{
int a;
int sum = 0;
String userinput;
userinput = JOptionPane.showInputDialog( "Enter a number. " , null);
a = Integer.parseInt(userinput);
for (int i = 0; i <= a; i++)
{
if (i % 2 == 1)
{
sum += i;
}
}
System.out.println( sum );
}
}
The sum always prints a large number, but I do not know how to make it print the correct number. Please help. Thanks.