aspose file tools*
The moose likes Beginning Java and the fly likes It's reading things wrong Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "It Watch "It New topic
Author

It's reading things wrong

Emily Smith
Greenhorn

Joined: Feb 23, 2013
Posts: 3
I'm trying to write a program that determines whether or not a number is a multiple of 3, using the "add the digits together, see if sum is divisible by 3" trick. So far, I've got:




But every time I try to run that, I get a huge number that's completely off and I realized that the number being added is the ASCII code (is that what it's called?). How do I get my program to read the numbers as integers instead of characters?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Welcome to the Ranch. Please paste the whole code inside the code tags. That way it becomes much more legible.


~ Mansukh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Welcome again. I have added the code tags as described here, and it looks a lot better.
You can try the numericValue method of the Character class, but you are better using the Integer class method which parses a String to an int. Then you need to know how to iterate the individual digits, possibly best done with a combination of the % and / operators.
Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

Campbell Ritchie wrote:Welcome again. I have added the code tags as described here, and it looks a lot better.
You can try the numericValue method of the Character class, but you are better using the Integer class method which parses a String to an int. Then you need to know how to iterate the individual digits, possibly best done with a combination of the % and / operators.


I'd probably do it this way:

1. Iterate each character of the String
2. parse each String character to, for example int x
3. Add the x to int total

But I'd like to know how to iterate each int digit using the % and / operators.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
There are simpler ways to get numeric values than parsing a single char. If yo uare sure all the numbers will be decimal (or a smaller radix) you can use
c - '0'
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: It's reading things wrong