473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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:
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.  
Mar 20 '08 #1
2 2502
sukatoa
539 512MB
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
Mar 20 '08 #2
satch
23
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.
Mar 20 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Kevin T. Ryan | last post by:
Hi All - I'm not sure, but I'm wondering if this is a bug, or maybe (more likely) I'm misunderstanding something...see below: >>> f = open('testfile', 'w') >>> f.write('kevin\n') >>>...
4
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
9
by: Alex Buell | last post by:
I have a small text file which consist of the following data: ]] And the code I've written is as follows: ]] The trouble is, I can't work out why it goes into an infinite loop reading the...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
42
by: psbasha | last post by:
Hi, Is it necessary in Python to close the File after reading or writing the data to file?.While refering to Python material ,I saw some where mentioning that no need to close the file.Correct me...
2
by: Clive Green | last post by:
Hello peeps, I am using PHP 5.2.2 together with MP3_Id (a PEAR module for reading and writing MP3 tags). I have been using PHP on the command line (Mac OS X Unix shell, to be precise), and am...
4
by: pbj2009 | last post by:
Hello all: I'm pretty stumped on this one. I'm not looking for Code, I'm just trying to figure out the best way to start this since I am new to reading and writing from files. I can't figure out...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.