Connecting Tech Pros Worldwide Help | Site Map

Reading and Writing a Text File???

Newbie
 
Join Date: Mar 2008
Posts: 10
#1: Mar 20 '08
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:
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.io.File;
  3. import java.io.PrintWriter;
  4.  
  5. public class SumFile 
  6.   {
  7.   public static void main( String[] args ) throws Exception 
  8.   {
  9.  
  10.     File f = new File("data.txt");
  11.  
  12.     Scanner input = new Scanner(f);
  13.  
  14.     String name;
  15.     int number;
  16.     int sum;
  17.  
  18.  
  19.     while ( input.hasNext() )
  20.     {
  21.       number = input.nextInt();
  22.       number = input.nextInt();
  23.       number = input.nextInt();
  24.       number = input.nextInt();
  25.       number = input.nextInt();
  26.       number = input.nextInt();
  27.  
  28.       sum= number + number;
  29.  
  30.      System.out.printf("The file sum is " + sum);
  31.     }
  32.     File g = new File("result.txt");
  33.  
  34.     PrintWriter output = new PrintWriter(g);
  35.     output.println
  36.  
  37.  
  38.     input;.close();
  39.   }
  40. }
  41.  
Needs Regular Fix
 
Join Date: Nov 2007
Location: Cebu City, Philippines
Posts: 508
#2: Mar 20 '08

re: Reading and Writing a Text File???


Quote:

Originally Posted by ccarter45

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:

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.io.File;
  3. import java.io.PrintWriter;
  4.  
  5. public class SumFile 
  6.   {
  7.   public static void main( String[] args ) throws Exception 
  8.   {
  9.  
  10.     File f = new File("data.txt");
  11.  
  12.     Scanner input = new Scanner(f);
  13.  
  14.     String name;
  15.     int number;
  16.     int sum;
  17.  
  18.  
  19.     while ( input.hasNext() )
  20.     {
  21.       number = input.nextInt();
  22.       number = input.nextInt();
  23.       number = input.nextInt();
  24.       number = input.nextInt();
  25.       number = input.nextInt();
  26.       number = input.nextInt();
  27.  
  28.       sum= number + number;
  29.  
  30.      System.out.printf("The file sum is " + sum);
  31.     }
  32.     File g = new File("result.txt");
  33.  
  34.     PrintWriter output = new PrintWriter(g);
  35.     output.println
  36.  
  37.  
  38.     input;.close();
  39.   }
  40. }
  41.  

You can just use Formatter for writing file....(Useful for debugging)
And Scanner for reading...

Your implementation is confusing....
When you read the data inside the file, you must store them in different variable...

You are just simply storing the values in a variable only...

For example,

1 2
2 3
3 4 -> the node is on this now....Assuming....

The number variable gets the 4 (last value before EOF)

And you are just summing the same value 4 + 4 = 8
You cannot say 3 + 4 = 7 Base on your code posted....

Maybe like this:

Expand|Select|Wrap|Line Numbers
  1. datatype sum set zero
  2.  
  3. while( StillHasValueAtNextPointing ) {
  4.        num1 = firstvalueCurrentLine
  5.        num2 = secondValueCurrentLine
  6.        sum += ( num1 + num2 )
  7. }
  8.  
  9. Save the sum into another file....

Correct me if im wrong,
Sukatoa
satch's Avatar
Newbie
 
Join Date: Feb 2008
Location: bangalore, karnataka, India
Posts: 23
#3: Mar 20 '08

re: Reading and Writing a Text File???


Quote:

Originally Posted by ccarter45

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:

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.io.File;
  3. import java.io.PrintWriter;
  4.  
  5. public class SumFile 
  6.   {
  7.   public static void main( String[] args ) throws Exception 
  8.   {
  9.  
  10.     File f = new File("data.txt");
  11.  
  12.     Scanner input = new Scanner(f);
  13.  
  14.     String name;
  15.     int number;
  16.     int sum;
  17.  
  18.  
  19.     while ( input.hasNext() )
  20.     {
  21.       number = input.nextInt();
  22.       number = input.nextInt();
  23.       number = input.nextInt();
  24.       number = input.nextInt();
  25.       number = input.nextInt();
  26.       number = input.nextInt();
  27.  
  28.       sum= number + number;
  29.  
  30.      System.out.printf("The file sum is " + sum);
  31.     }
  32.     File g = new File("result.txt");
  33.  
  34.     PrintWriter output = new PrintWriter(g);
  35.     output.println
  36.  
  37.  
  38.     input;.close();
  39.   }
  40. }
  41.  

You should realize that in lines 22 to 26 of your code you are replacing the value stored in variable 'number' with a new value. Now this does not make sense when you want to use every value that you read from the file.

Every time you assign a new value to the variable 'number' you loose the previous value, so you better use it before overwriting it. And if you want to use two value simultaneously in some statement then you should store them in two different variables.
Reply