Results 1 to 13 of 13
  1. #1
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default 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();
    		}
    
    	}
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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.

  4. #4
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default

    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)

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You cannot do this.

    Java Code:
    fullName = enter.substring(0, enter.indexOf("\n")).toUpperCase();
    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.

  6. #6
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default

    Please enter your full name: Fullname
    Please enter your location: Location
    Please enter your age: 20
    The Students.txt is like this: FullnameLocation20

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by mrdestroy View Post
    The Students.txt is like this: FullnameLocation20
    Are you sure about this. Seems to me you are trying to write those values in different lines.

  8. #8
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default

    Yes, I want to write those like this:
    Fullname
    Location
    20

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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. #10
    mrdestroy is offline Member
    Join Date
    Oct 2008
    Posts
    22
    Rep Power
    0

    Default

    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?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    How can I fix the second half code above to print those on screen?
    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.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by mrdestroy View Post
    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?
    The best way for me is use of system properties. Do can do it like this.

    Java Code:
                    print.println(fullName + System.getProperty("line.separator") 
                            + location + System.getProperty("line.separator") + age);

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by mrdestroy View Post
    How can I fix the second half code above to print those on screen?
    As I said earlier you cannot do this.

    Java Code:
    fullName = enter.substring(0, enter.indexOf("\n")).toUpperCase();
    You cannot find the newline character index.

    In simple way you can do it like this.

    Java Code:
                while (true) {
                    enter = inputFile.readLine();
                    if (enter == null)
                        break;
                    
                    output = output + "\n" + enter;
                }
                JOptionPane.showMessageDialog(null, output);
    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.

Similar Threads

  1. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM
  2. Writing to files within jar files
    By erhart in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2008, 02:50 AM
  3. Text and image files within jar files
    By erhart in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2008, 04:43 AM
  4. how to convert mpeg files to .wav files
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:14 AM
  5. convert xls files into pdf files
    By bbq in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 03:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •