Read Data From Text File And Sum Numbers

前端 未结 8 1454
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 13:59

I want to read in data from a text file which is full of integers and have the program print those integers out to the screen while summing them. This shouldn\'t be hard, b

相关标签:
8条回答
  • 2020-12-21 14:25

    Because you never increment i, it never changes from 0, so it is always less than 25. That means it tries to look again after it has done 25 elements, but there is no 26th, so there's an exception.

    Instead of a while loop, I would recommend a for loop.

    To sum them, instead of printing them out, just add to an accumulator.

    int accumulator = 0;
    for(int i = 0; i < 25; i++)
        accumulator += aScanner.nextInt();
    
    System.out.println(accumulator);
    

    should print out the sum.

    0 讨论(0)
  • 2020-12-21 14:28

    You are getting the error (exception) because your file has very big numbers that do not fit on Integer types. You should use Long.

    So, use: aScanner.nextLong() instead of aScanner.nextInt().

    EDIT: You should also change the type of the sum variable from int to long.

    Your code will work fine.

    0 讨论(0)
  • 2020-12-21 14:29

    You are calling textfile.nextInt() twice in the loop. Try:

    static void filereader(Scanner textfile)     
    {         
        int i = 0;         
        int sum = 0;          
        while(i <= 19)         
        {       
            int nextInt = textfile.nextInt();          
    
            System.out.println(nextInt);             
            sum = sum + nextInt;
            i++;         
        }     
    }
    
    0 讨论(0)
  • 2020-12-21 14:31

    You never increment i, so the while loop continues beyond the end of the file. As for summing, do something like this.

    static void addstuff(Scanner aScanner) {
            int sum = 0;
    
            while (aScanner.hasNextInt()) {
                sum += aScanner.nextInt();
            }
    
            System.out.println(sum);
    }
    

    This will run until hasNextInt() returns false, which occurs when the next item the scanner sees isn't an integer. This could be non-numeric input or the end of the file. This means you're no longer limited to 25 integers - it'll read as many as it can before stopping.

    0 讨论(0)
  • 2020-12-21 14:44

    You will have to do following thing

        while(i < 25)
        {
            System.out.println(aScanner.nextInt());
            i++ ;
        }
    
    0 讨论(0)
  • 2020-12-21 14:44

    It throws an exception because you don't increment i in the loop. In case if the number of integers in the input file is unknown, you can use the Scanner.hasNextInt() method to check the presence of the next integer (or just use hasNext() to check for EOF).

    You can sum the numbers like in the code snippet below:

        ...
        int sum = 0;
        while(i++ < 25)
        {
            sum += aScanner.nextInt();
        }
    

    or

        ...
        int sum = 0;
        while(aScanner.hasNextInt())
        {
            sum += aScanner.nextInt();
        }
    
    0 讨论(0)
提交回复
热议问题