Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prompt for file name

 
Luigi Melanson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to alter this code to have it prompt for a file name instead of predefining it in the code ("filein.txt")
Any help would be appreciated,
The code is below:

import java.io.*;
class InputExcercise
{
public static void main(String args[])throws Exception
{
FileReader fr = new FileReader("filein.txt");
FileWriter fw = new FileWriter("fileout.txt");

int ch;
ch = fr.read();
while(ch != -1)
{
if((char)ch >='A' && (char)ch <='Z')ch = ch +('a'-'A');
fw.write((char)ch);
ch = fr.read();
}
fr.close();
fw.close();
}
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Luigi Melanson:
I am trying to alter this code to have it prompt for a file name...

Take a look at java.util.Scanner.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Luigi Melanson:
.... prompt for a file name instead of predefining "filein.txt" in the code
Any help would be appreciated,

I Think I understand what you are wrestling with here. The cite Marc Weber provides is certianly something you need to keep on your list, but the problem is asking the user for a filename then reading that in.

I do not know how to read the standard input, but you have the option of having the user type the name of the file when they start the program:
at the command line when they start the program. This is one way to do it:

This now has a File object that literally uses the name that was typed in. It is not opened or anything, look through http://java.sun.com/j2se/1.3/docs/guide/io/io-maj.html very carefully to get your bearings before trying to do anything with the file. The Scanner Marc mentioned will more than likely read() what the user types in, but you will have to read up on it to use it. To ask the user for a file name, you do this:I prefer my programs not to sit and wait on the user to type in something, it better be there when they invoke the program.

Either way, it looks to me what you are doing is struggling with what to do with the stuff the user types in, as a string problem rather than the problem you describe. I know it well, hung for a waste of time seemingly getting nothing done.

This is called "Re-inventing the wheel." in computer work.


[ December 24, 2006: Message edited by: Nicholas Jordan ]
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the scanner class to read in a string as a filename.



Make sure you catch the "FileNotFoundException"...
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic