Forums Register Login

Help with java input file

+Pie Number of slices to send: Send
Please
I need help with this code. I want to be able to change "questionFile.txt" to correspond to the name of any file I input into the application.



try {

BufferedReader inputfile = new BufferedReader( new FileReader("questionFile.txt"));
for ( int i = 0; i < 3; i++){
question[i] = inputfile.readLine();
answer1[i] = inputfile.readLine();
answer2[i] = inputfile.readLine();
answer3[i] = inputfile.readLine();
answer4[i] = inputfile.readLine();
answer5[i] = inputfile.readLine();
}

inputfile.close();
}catch (IOException e) {
System.err.println(e);
System.exit(1);
}
+Pie Number of slices to send: Send
jonas,

you are using the "FileReader(String fileName)" constructor in "new FileReader("questionFile.txt"))". So any string will do.
+Pie Number of slices to send: Send
jonas,
Pass the file name you want to read as Input parameter to main pgm like
> java pgmname "input file".
In your main function read it using args[0] into a string say strFile, where args[] is the main() function argument.
In BufferedReader constructor, you say -
BufferedReader br = new BufferedReader(strFile);
Hope this helps you out.
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 975 times.
Similar Threads
Reading from a file to a bidimensional array
Need help with username and password validation
Help with java input file
Problem with getDocumentBase()
reading from file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:31:06.