Reading and Writing a Text File???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ccarter45
    New Member
    • Mar 2008
    • 10

    Reading and Writing a Text File???

    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:
    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();
      }
    }
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    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:
    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();
      }
    }
    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:

    Code:
    datatype sum set zero
    
    while( StillHasValueAtNextPointing ) {
           num1 = firstvalueCurrentLine
           num2 = secondValueCurrentLine
           sum += ( num1 + num2 )
    }
    
    Save the sum into another file....

    Correct me if im wrong,
    Sukatoa

    Comment

    • satch
      New Member
      • Feb 2008
      • 23

      #3
      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:
      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();
        }
      }
      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.

      Comment

      Working...