aspose file tools*
The moose likes Beginning Java and the fly likes A question about Int Values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A question about Int Values" Watch "A question about Int Values" New topic
Author

A question about Int Values

Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
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 ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You can break it up using the modulo % operator.

250 % 10 = 0
250 / 10 = 25 //(int division)

25 % 10 = 5
25 / 10 = 2 //(int division)

2 % 10 = 2
2 / 10 = 0 //(int division)

Garrett


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
I am confused of what is going on it almost works 100% expect for 1 thing.

if I enter in 0123456789 I get:

"your value you ented contained: 4 even number(s) 5 odd number(s) and 0 zero(s) "

That is of course wrong since it is missing the total tally count of 1 zero:

if I enter 0123456789 I get this:

"your value you ented contained: 4 even number(s) 5 odd number(s) and 1 zero(s)"

So that is right.

So this is my code:



I don't know why it does not like reading in the first zero and counting it.
[ February 18, 2006: Message edited by: Michael Hubele ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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 ]
Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
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 ]
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
Technically, 1 and 01 and 0000000001 can be treated as same, just like 2.00 and 2.0. So, i dont think that should be much of a problem.


SCJP 1.4 85%
 
wood burning stoves
 
subject: A question about Int Values