Help! I need to write a program that reads a text file with numbers and sums up each line of numbers. Then write a part of the program that outputs the text to another file. How do I do this?
Here's what the data file looks like:
1 2
2 3
3 4
4 5
5 6
Here is the code I have:
Here's what the data file looks like:
1 2
2 3
3 4
4 5
5 6
Here is the code I have:
Code:
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
public class SumFile
{
public static void main( String[] args ) throws Exception
{
File f = new File("data.txt");
Scanner input = new Scanner(f);
String name;
int number;
int sum;
while ( input.hasNext() )
{
number = input.nextInt();
number = input.nextInt();
number = input.nextInt();
number = input.nextInt();
number = input.nextInt();
number = input.nextInt();
sum= number + number;
System.out.printf("The file sum is " + sum);
}
File g = new File("result.txt");
PrintWriter output = new PrintWriter(g);
output.println
input;.close();
}
}
Comment