January 31st, 2003, 01:25 AM
Please someone help me.
I suppose to use file input and output. Now I am able to read file, but I can't get print out the data to the output file. I made a file, but nothing there..... I have no idea what to do... also I have to sort the data by last name....
this is my code.
import java.util.*; //for Stringtokenizer class
public Child(String n, String sn, String gp, int a)
name = new String(n);
surname = new String(sn);
group = new String(gp);
age = a;
public String toString()
return( name + " " + surname + "\n"
+"Group: " + group + "\n"
+ "Age: " + age );
} //end Child class
public static void main(String args) throws IOException
for( int i = 0; i < args.length; i ++ )
listfile( args[i] );
public static void listfile( String fileName ) throws IOException
Child children = new Child;
int nchildren = 0;
BufferedReader fileIn = null;
PrintWriter fileOut ;
fileIn = new BufferedReader( new FileReader(fileName) );
fileOut = new PrintWriter( new FileWriter("Name.txt" ));
fileOut.println( " Name List ");
catch(IOException e )
while ( nchildren < 3)
inputLine = fileIn.readLine();
info = new StringTokenizer(inputLine);
if(info.countTokens() != 4)
throw new IOException();
String nm = info.nextToken();
String sn = info.nextToken();
String gp = info.nextToken();
int a = Integer.parseInt(info.nextToken());
children[nchildren] = new Child(nm,sn,gp,a);
catch ( Exception e)
for(int j = 0; j < 3; j++)
System.out.println(children[j] + "\n");
} //end Kindergarten
I know I am missig some code, but I can't even get the words "Name List".
I have no idea, so please help me.
January 31st, 2003, 02:46 PM
I assume you read my reply to your other post about the sorting. You are going to have to implement a custom sort algorithm using the Arrays class. The documentation has directions.
http://java.sun.com/j2se/1.4/docs/ap...il.Comparator) look at the sort(Object a, Comparator c) method
The reason you are getting no output to the Name.txt file is because you are not flushing or closing the stream. The program ends and the contents you have written are still in a buffer and never written. Incidentally, you never close the input stream either, which is very bad form.
When you are sure you are done with the output stream call output.flush() and ouput.close(). When you are sure you are done with the input stream make sure and close it also by calling input.close().
Last edited by Nemi; January 31st, 2003 at 02:51 PM.