-1

I'm getting the user to enter a string of numbers, then a for loop should pick out each number from the string and add it to an ArrayList. I'm sure someone can help me out fairly quickly

My problem is as follows. When I print out all the values in the ArrayList, It is printing out much higher numbers e.g. 1234 = 49 50 51 52.

I think what is happening is that it is printing out the ASCII values rather than the numbers themselves. Can anyone spot where and why this is happening?

I have tried changing the int variable barcodeNumberAtI to a char, which yields the same result.

Apologies for lack of comments but this was only supposed to be a quick program

int tempNewDigit;
String barCode, ans;
int barcodeNumberAtI;
ArrayList <Integer> numbers = new ArrayList <Integer>();


public void addNumbers(){

    Scanner s = new Scanner(System.in);

    do{
    System.out.println("Please enter a 12 digit barcode\n");
    barCode = s.nextLine();

    for(int i = 0; i < barCode.length(); i++){

        barcodeNumberAtI = barCode.charAt(i);
        System.out.println(barcodeNumberAtI);
        numbers.add(barcodeNumberAtI);

    }

    System.out.print("Would you like to add another? y/n\n");
    ans = s.nextLine();
    } while (!ans.equals("n"));     
}

public void displayNumbers(){

    for(int i = 0; i < numbers.size(); i++){
        System.out.print(numbers.get(i));
    }

} 
Davide Spataro
  • 7,319
  • 1
  • 24
  • 36
Dylan
  • 454
  • 2
  • 16
  • The barCode entered by user is always a number? – Vadym Pechenoha Jul 26 '17 at 16:57
  • It is supposed to be yes, I just have not added any checks to make sure that it is – Dylan Jul 26 '17 at 16:58
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Jul 26 '17 at 16:59
  • barcodeNumberAtI should be a char, not an int. You say you tried that and it didn't work, but at present if it is an int it will be converting your chars to ints, which in java gets their ascii value. Also, your ArrayList should be of chars, not ints. – Chris Cousins Jul 26 '17 at 17:01
  • @JarrodRoberson i haven't been coding long enough to know how and where to use debuggers – Dylan Jul 26 '17 at 17:02
  • @ChrisCousins I originally had a cast to add the chars to the arrayList, that is making sense now. Should I make the list a list of strings instead? – Dylan Jul 26 '17 at 17:04
  • @Dylan - then now is the time to learn, did you even read the link? –  Jul 26 '17 at 17:17
  • Apologies, it wasn't a link for me originally. I will have a look at it now – Dylan Jul 26 '17 at 17:36

1 Answers1

2

Happens at this line: barcodeNumberAtI = barCode.charAt(i);

barCode.charAt(i) returns a char which is converted to a int by using its ASCII value.

Use this instead:

barcodeNumberAtI = Character.digit(barCode.charAt(i), 10);

What Character.digit does is converting its first argument from the type char to the corresponding int in the radix specified by the second argument. Here's a link to the documentation

Davide Spataro
  • 7,319
  • 1
  • 24
  • 36