Results 1 to 13 of 13
Thread: Need help with Files
- 10-24-2008, 06:01 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 22
- Rep Power
- 0
Need help with Files
I cannot run my code, pls help me to find the reason:
Java Code:public static void main(String[] args) { String fullName, location, exit, enter, output = ""; int age; try { FileWriter outputFile = new FileWriter("Students.txt"); PrintWriter print = new PrintWriter(outputFile); do { fullName = JOptionPane .showInputDialog("Please enter your full name: "); location = JOptionPane .showInputDialog("Please enter your location: "); age = Integer.parseInt(JOptionPane .showInputDialog("Please enter your age: ")); print.println(fullName + "\n" + location + "\n" + age); exit = JOptionPane.showInputDialog(null, "Do you want to continue? (Yes or No)"); } while (exit.equalsIgnoreCase("Yes")); outputFile.close(); print.close(); } catch (IOException e) { e.printStackTrace(); } try { FileReader fReader = new FileReader("Students.txt"); BufferedReader inputFile = new BufferedReader(fReader); while (true) { enter = inputFile.readLine(); if (enter == null) break; fullName = enter.substring(0, enter.indexOf("\n")).toUpperCase(); enter = enter.substring(enter.indexOf("\n")+1); location = enter.substring(0, enter.indexOf("\n")); age = Integer.parseInt(enter.substring(0, enter.indexOf("\n")).trim()); output += fullName + "\n" + location + "\n" + age; } JOptionPane.showMessageDialog(null, output); fReader.close(); inputFile.close(); } catch (Exception e) { e.printStackTrace(); } } }
- 10-24-2008, 06:58 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you mean cannot run your code? Any error message you get when you run it. If so copy-paste it here to see. You must explain much clearly about your question.
- 10-24-2008, 06:59 PM #3
The first half works okay. What does your Students.txt file look like?
The second half of your app is trying to parse lines of this file with the String substring method and it looks like this may be an area of difficulty.
- 10-24-2008, 07:06 PM #4
Member
- Join Date
- Oct 2008
- Posts
- 22
- Rep Power
- 0
oh, my bad, I forgot to post the error, I try to fix but still not working. I think the second Try catch works wrongly. The program cannot read the text.
Java Code:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(Unknown Source) at Problem1.main(Problem1.java:49)
- 10-24-2008, 07:17 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You cannot do this.
How can you detect the new line character this way. It's wrong. Seems to me on the first part you are trying to write some data to a text files, seems ok for me in some distance. Check the file after running the application. I'm sure you'll see some annoying character somewhere in that text file. Because your defining of new line is not good at all.Java Code:fullName = enter.substring(0, enter.indexOf("\n")).toUpperCase();
- 10-24-2008, 07:17 PM #6
Member
- Join Date
- Oct 2008
- Posts
- 22
- Rep Power
- 0
Please enter your full name: Fullname
Please enter your location: Location
Please enter your age: 20
The Students.txt is like this: FullnameLocation20
- 10-24-2008, 07:18 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 10-24-2008, 07:55 PM #8
Member
- Join Date
- Oct 2008
- Posts
- 22
- Rep Power
- 0
Yes, I want to write those like this:
Fullname
Location
20
- 10-24-2008, 08:20 PM #9
Use the newline character at the end of every String to put the next String on a new line. Or use the println() method.
Your code has those techniques, so are all the Strings being concat in the first string and the next 2 strings are empty so your output is three lines, one with all the data followed by 2 empty lines.
Put delimiting characters around each string in your println():
println(">" + name + "<\n>" + addr + "<\n>" + age + ",");
to show you where the values are.
- 10-24-2008, 09:15 PM #10
Member
- Join Date
- Oct 2008
- Posts
- 22
- Rep Power
- 0
I changed this
print.println(fullName + "\n" + location + "\n" + age);
to
print.println(fullName);
print.println(location);
print.println(age);
the text now is:
Fullname
Location
20.
(those are just in file .txt)
How can I fix the second half code above to print those on screen?
- 10-24-2008, 10:04 PM #11
It would be better if you copied and noted the code you want to change rather than have people guess what lines of code you want to change.How can I fix the second half code above to print those on screen?
- 10-25-2008, 06:19 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 10-25-2008, 06:40 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
As I said earlier you cannot do this.
You cannot find the newline character index.Java Code:fullName = enter.substring(0, enter.indexOf("\n")).toUpperCase();
In simple way you can do it like this.
Then you have to think about a design pattern to handle that full name upper case convention and so on. Easiest way to do it is before write them into the file convert into upper case letters.Java Code:while (true) { enter = inputFile.readLine(); if (enter == null) break; output = output + "\n" + enter; } JOptionPane.showMessageDialog(null, output);
Similar Threads
-
Behaving text files like binary files
By Farzaneh in forum New To JavaReplies: 2Last Post: 08-27-2008, 03:20 PM -
Writing to files within jar files
By erhart in forum Advanced JavaReplies: 0Last Post: 02-04-2008, 02:50 AM -
Text and image files within jar files
By erhart in forum Advanced JavaReplies: 8Last Post: 01-19-2008, 04:43 AM -
how to convert mpeg files to .wav files
By christina in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:14 AM -
convert xls files into pdf files
By bbq in forum New To JavaReplies: 3Last Post: 07-20-2007, 03:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks