
February 27th, 2008, 12:47 AM
|
|
Registered User
|
|
Join Date: Dec 2007
Posts: 6
Time spent in forums: 44 m 58 sec
Reputation Power: 0
|
|
|
[Homework] Trying to display name + sum of numbers from file
ok well let me just explain what the program needs to do.
Write a program that will input data from a file and calculate an average. Each line of the file will have a single line followed by series of numbers. calculate the average of the numbers. Write the name and average out to another file...
now here is what I have, this will basically input a file and output the same thing to a different file name..
Code:
import java.io.*;
import java.util.*;
public class FileIOedit
{
public static void main(String args[])
{//begin write to file method
FileReader inFile;
BufferedReader bFile;
FileWriter fw;
BufferedWriter w;
String s, word, temp = "";
String inputFile = "Inpu.dat";
StringTokenizer line;
try
{
//Open the input stream for reading
inFile = new FileReader(inputFile);
bFile = new BufferedReader (inFile);
//Open the output file for writing
fw = new FileWriter("test.dat");
w = new BufferedWriter (fw);
//get first line of input file
s = bFile.readLine();
//while their are more lines in the input file
while (s != null)
{
line = new StringTokenizer(s);
//while there are more words in this line
while(line.hasMoreTokens())
{
word = line.nextToken();
//int num = Integer.parseInt(word);
//if this word is going to overflow the output line
temp = temp + word + " ";
}
//get next line from the input file
s = bFile.readLine();
}
//write the last line to the output file, flush and close buffer
w.write(temp);
w.flush ();
w.close ();
}//end try
catch(IOException exception)
{
System.out.println("Hey you idiot!!!");
System.out.println(exception);
}
}//end main method
}//end class
what im trying to do from here is figure out how have the name separate from the actual numbers (I think I need to use strings???) but im confused on how to convert the actual text over to two different data types
im assuming you would use something like this
Code:
len = Namebefore.length();
position = Namebefore.indexOf(" ");
firstName = Namebefore.substring(0, position);
lastName = Namebefore.substring(position +1, len);
any help would be greatly appreciated
~Dan~
|