Win a copy of Clojure in Action this week in the Clojure forum!
    Bookmark Topic Watch Topic
  • New Topic

Integer values (Not ASCII) from within a String

 
Phil Lewis
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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??
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic