English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

The problem was to write an application that determines and prints the number of odd, even, and zero digits in an integer value read from the keyboard.

i know how to do it but my question is that how do i find out the amount of digits in the number that was inputted by the user in the scanner. for ex:

for (int count=0; count <= XXXXX; count++)
{

if (b==0)
zeroCounter++;
else if (b % 2 == 0)
evenCounter++;
else (b % 2 == 1)
oddCounter++;

b +/ 10;
}

2007-10-22 11:01:18 · 2 answers · asked by Filipinocalypse 3 in Computers & Internet Programming & Design

2 answers

If I'm not mistaken, any value that comes in from user input comes in as a string, and needs to be parsed to an integer or double to be used in calculations. So, when you first receive the variable in, I would use a built in string function (depending on language, it will be different - like Len(myString), or myString.Length) to get the length, then use advanced string functions to extract each digit, convert it to a numeric type, then do your operations. If you try this, and still can't get it going, let me know (email), and I will be happy to help. Good luck : )

2007-10-22 12:19:17 · answer #1 · answered by Anonymous · 0 0

I would just turn the number into a string.

java.lang.String numberString=Integer.toString(count);

Then, get the length:

int numberStringLength=numberString.length();

If you need to find the number of zeros, loop through the string and find the characters that == '0'.

int numberOfZeroes=0;
for(int i=0;i {
if(numberString.charAt(i)=='0')
{
numberOfZeroes++;
}
}

2007-10-25 10:48:25 · answer #2 · answered by godfatherofsoul 7 · 0 0

fedest.com, questions and answers