• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Reading input and writing the output

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic