• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A question about Int Values

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
A teeny tiny vulgar attempt to get you to buy our stuff
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic