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;
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??
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.