This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
I am doing an assignment for class I basicly have to read a interger value from the keyboard in by the user. Then disect it find out how many odds,evens, and zeros are in it.
So if they enter in 250 then that is 1 even, 1 odd, 1 zero.
I written the input part, and the if statement to test what it would be. My problem is that I am not sure how to break it up so that it reads each number in seperatley.
Since if the user enters 20 in or something like that how I have it now it will just say this is just 1 even number. So my program can do figure it 100% from 0-9 but after that it just reads it as one big digit.
So I am wondering if there is away to break a int up to each character. Like I know if this was a String I could just use the length()-1 method with CharAt() with a loop and use a while loop to go through one by one and this while loop would have all my if statements in it.
But I am not sure if something exists for that for a int value. I would use a String but then it would allow people to put in words and that that would screw up my whole program unless I wrote something that would not allow this. Also how the question is worded it wants me to take a integer value in so if I do the String way I won't be doing this so I don't even think I would be allowed to do this.
[ February 18, 2006: Message edited by: Michael Hubele ]
[ February 18, 2006: Message edited by: Michael Hubele ] [ February 18, 2006: Message edited by: Michael Hubele ]
When you read an int or parse a string into an int you lose any leading zeroes. Try keeping it all in string format and examining each character one at a time. Look at String and see if you spot a way to turn it into an array of something numeric or get one character at a time.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Jan 17, 2006
The nextInt() strips the leading zeros as they are insignifigant
1 == 001 == 0000000000000001
If you want to retain the leading zeros, that not so trivial a task. You could read in the input as a String, then parse each individual digit as a substring of the input String (after of course checking to ensure each character is a digit), and store digits in an int array. Then you could count the number of zeros, odds, evens, etc. in the array. [ February 18, 2006: Message edited by: Garrett Rowe ]
Joined: Dec 17, 2005
Oh I never knew that a int cut off the leading zeros. Ok well then I will just leave it since first I got to scan it as a int in and since you both say you need to use an array I highly doubt I would need to do this since this assignement is only for chapter 4 & 5(loops and etc) and array's we will learn in chapter 7 so about 2 weeks away(about a week after this assignment is due).
So I will just leave it is as it is.
I will now remove my code just incase anyone else has to do this exact assignment. [ February 18, 2006: Message edited by: Michael Hubele ]