Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 31st, 2003, 12:25 AM
pinky79 pinky79 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 3 pinky79 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
File I/O

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.io.*;
import java.util.*; //for Stringtokenizer class

class Child
{
String name;
String surname;
int age;
String group;

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

class Kindergarten
{
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[3];
int nchildren = 0;

BufferedReader fileIn = null;
PrintWriter fileOut ;
StringTokenizer info;
String inputLine;

try
{
fileIn = new BufferedReader( new FileReader(fileName) );
fileOut = new PrintWriter( new FileWriter("Name.txt" ));

fileOut.println( " Name List ");

}
catch(IOException e )
{
System.out.println(e);
System.exit(1);
}


while ( nchildren < 3)
{
try
{
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);
nchildren++;

}
catch ( Exception e)
{
System.out.println(e);
System.exit(1);
}
}

for(int j = 0; j < 3; j++)
{
System.out.println();
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.
thank you

Reply With Quote
  #2  
Old January 31st, 2003, 01:46 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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/a...til.Comparator) look at the sort(Object[] a, Comparator c) method

http://java.sun.com/j2se/1.4/docs/a...Comparator.html


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 01:51 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > File I/O


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT