By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,104 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,104 IT Pros & Developers. It's quick & easy.

Reading input and writing output

P: n/a
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 ) );
}

out.close();
}
*---------------------------------*
Posted at: http://www.GroupSrv.com
Check: http://www.HotCodecs.com
*---------------------------------*
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
slickn_sly wrote:
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"

====cut cut======

what kind of error do you get? at what line? try to be more specific,
maybe copy & paste error message.
suggestion: try to print on standard output first (System.out), once you
are sure it works well, print it to the file.
Are you sure the file is in the same directory/folder as your program?
btw ever used the java debugger? it's a handy tool sometimes very useful.

cheerz
oc
--
All our questions are porcelain
Jul 17 '05 #2

P: n/a
"slickn_sly" <bn***@hotmail-dot-com.no-spam.invalid> wrote in message
news:41**********@127.0.0.1...
[...]
You will get more replies if you direct your posts to comp.lang.java.help in the
future. This group is deprecated.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.