File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading input and writing the output

 
Mark Rem
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 172
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic