aspose file tools*
The moose likes Java in General and the fly likes Integer values (Not ASCII) from within a String 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 » Java in General
Reply locked New topic
Author

Integer values (Not ASCII) from within a String

Phil Lewis
Greenhorn

Joined: Apr 01, 2003
Posts: 4
I am trying to create a program which gets all the odd numbers from a barcode (Which is a String), and adds them together.
I have tried the code below, but instead of adding the integer values, it adds the ASCII values.
What is the easiest/best way of pulling the integers out of the String?
Many thanks

public static void main (String [] args)
{
String barcode = "1234567891234";
int number, theNumber, value=0;

for (number=0; number<12; number=number+2){
theNumber=barcode.charAt(number);
value=value+theNumber;

System.out.println("The Value is " +value);
}
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

check out the API that you're using:
String.charAt()
char charAt(int´┐Żindex)
Returns the character at the specified index.
--> it returns a char, not an int
Check out the API for Character -- is there anything there that takes a char and returns an int??


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Please don't post the same question in multiple forums - it wastes people's time when they don't realize a question has already been answered elsewhere. Follow up here. Thanks.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer values (Not ASCII) from within a String