aspose file tools*
The moose likes Beginning Java and the fly likes Reading input and writing the output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading input and writing the output" Watch "Reading input and writing the output" New topic
Author

Reading input and writing the output

Mark Rem
Greenhorn

Joined: Dec 11, 2004
Posts: 13
For some reason, I'm getting an error. It doesn't seem to be reading my "input.txt" file. I'm trying to read the "input.txt" file which consits of random integers and use insertion sort to sort them in order. Then, i want to have the sorted integers go the file "output.txt"

static final int MAX_NUM = 100;

public static void main( String[] args ) throws java.io.IOException
{
int[] num = new int[MAX_NUM];

FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader( fr );
PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter( "output.txt" ) ) );

int count = 0;
while ( count < MAX_NUM )
{
String line = br.readLine();
if( (line == null) || line.equals( "" ) )
break;
try
{
num[count] = Integer.parseInt( line );
count++;
}
catch( Exception e)
{
System.out.println( "ERROR" );
}
//System.out.println( line );
//line = br.readLine();
}


insertionSort( num );

for( int i = 0; i < count; i++ )
{
out.println( Integer.toString( num[i] ) );
}

out.close();
}
Robert Konigsberg
Ranch Hand

Joined: Jun 23, 2004
Posts: 172
Mark,

Please provide much more information. What output do you receive? Most likely you will receive an exception, which is a great guide. If you receive no exception, that says alot as well.

Also, have you tried using either a debugger, or using System.out.println statements in your code to see what the real values are? Try that and see what you find.


SCJP 1.4 (91%)<br />SCJD 1.4 (376/400, 94%)
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Mark Rem:
If you are seeing "ERROR", you're throwing away valuable information. Replace the println() line with "e.printStackTrace();" This will tell you what type of error (file not found, end of file, unreadable file) and the line number where it occured.

If you're not seeing "ERROR" and the program exits normally but your output is wrong, you need to tell us how it's wrong.

Do you have a linefeed at the end of the file after the last digit of the last number?
[ January 30, 2005: Message edited by: David Harkness ]
 
 
subject: Reading input and writing the output