Read Data From Text File And Sum Numbers

前端 未结 8 1450
伪装坚强ぢ
伪装坚强ぢ 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:44

    1. Your i is never Incremented.

    2. You are not adding up numbers.

    3. addstuff(diskScanner) is a static memeber, so call it with class name with dot operator.

    Eg:

    Add.addstuff(diskScanner);
    
       static void addstuff(Scanner aScanner)
    {
        int i = 0;
        long l = 0;
    
    
        while(i < 25)
        {
            l = i + l;
            i++;
        }
    
        System.out.println(l);
    
    
    
    }
    
    0 讨论(0)
  • 2020-12-21 14:46

    In addition to lockstock's answer, you might want to consider adding textfile.hasNext() OR textfile.hasNextInt() for your while loop.

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

    }

    0 讨论(0)
提交回复
热议问题