1

45 90 40 30 42 95 64 47 23

14 80 30 84 24 49 16 10 20

I am beginner in Java OOP
This is the Txt file. I have to print sum of integers next to each line
and then find out the largest sum among them.

I can read the file
I tried by making each line as an String
I tried by making each line as an Int
but When I am making each digit of the line as an Int.
I couldn't able to figure out how to stop the program at the point where the column is ending so that I can print the sum and go to the next row.

import java.util.Scanner;
import java.io.*;

public class Asst1Problem2{


   public static void main(String[] args){

   int sum = 0;
   int largestSum = 0;

   String line = "";

   try{

      Scanner input = new Scanner(new File("integers2.txt"));


      while(input.hasNextLine()){

         line = input.nextLine();

         System.out.println(line);


      }

   }

   catch(FileNotFoundException fnf){

         System.out.print("no file "  + fnf);

   }

   }
} 

Expecting to get the sum but i am not getting it right.

Stephan Hogenboom
  • 1,543
  • 2
  • 17
  • 29

2 Answers2

1

Hi you can tackle this problem with loops or streams (java 8+). I also recommend you to use the try-with-resources.

Without streams you can use the following example


public static void main(String[] args) {
    int largestSum = 0;
    try (Scanner input = new Scanner(new File("integers2.txt"))) {
        while (input.hasNextLine()) {
            String[] numbers = input.nextLine().split(" ");
            int sum = 0;
            for (String s : numbers) {
                if (s.matches("^\\d+$")) { // check if the element was an integer number. 
                    int i = Integer.parseInt(s);
                    sum += i;
                }
            }
            System.out.printf("current sum = %s \\n", sum); // print current sum
            largestSum = Math.max(sum, largestSum);
        }
    } catch (FileNotFoundException fnf) {
        System.out.print("no file " + fnf.getMessage());
    }
    System.out.println(largestSum);
}

For more details regarding the check if a string is an integer

With java-8+ streams the following code should do the trick:

public static void main(String[] args) {
    int largestSum = 0;
    try (Scanner input = new Scanner(new File("integers2.txt"))) {
        while (input.hasNextLine()) {
            String[] numbers = input.nextLine().split(" ");
            int sum = Arrays
                            .stream(numbers)
                            .filter(s -> s.matches("^\\d+$"))
                            .mapToInt(s -> Integer.parseInt(s))
                            .sum();
            System.out.printf("current sum = %s \\n", sum); // print current sum
            largestSum = Math.max(sum, largestSum);
        }
    } catch (FileNotFoundException fnf) {
        System.out.print("no file " + fnf.getMessage());
    }
    System.out.println(largestSum);
}
Stephan Hogenboom
  • 1,543
  • 2
  • 17
  • 29
0
Scanner input = new Scanner(new File("integers2.txt"));

int maxSum = -1; //Assuming there are no negative values
while(input.hasNextLine()) {
   line = input.nextLine();
   String[] splitted = line.split(" "); //Split the spaces from the line

   int sum = 0;

   for (int i = 0; i < splitted.length; i++) {
       int value = Integer.parseInt(splitted[i]); //Convert the string into an integer
       sum += value;
   }

   System.out.println(sum); //This line sum

   if (sum > maxSum) {
       maxSum = sum;
   }
}

System.out.println(maxSum); //The biggest sum
Jose Nuno
  • 598
  • 3
  • 13