2

I try to read the numbers from the file with scanner. But I can't read one by one. How can i do that?

This is my txt

000000000000000000000000000000000000000

and this is my code

ArrayList<Integer>x = new ArrayList<>();

    Scanner scan;

    try {
        scan = new Scanner(new FileInputStream(new File("d:/deneme.txt")));
        while(scan.hasNext()){
            int y = scan.nextInt();
            System.out.println(y);
            x.add(y);               
        }
        scan.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Jørgen R
  • 10,568
  • 7
  • 42
  • 59
user3717742
  • 103
  • 1
  • 7

1 Answers1

6

When you read in nextInt, it treats 000000000000000000000 as 0. If you use String y = scan.next(), then parse by checking y.charAt(i) in a for loop you will check the digit in each location of the number string.

That char must then be parsed id you want to store it as an int, so use

int x = Character.getNumericValue(y.charAt(i));

Adam Yost
  • 3,616
  • 23
  • 36