Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Question Using Read/Write From File Using Scanner

    Ok, I'm created a rental car service program that allows the user to input their information and receive the results in a joptionpane dialog box. Now, I have to create a text file that will show the information exactly how the user has entered it and the same exact way that it shows up in the joptionpane dialog box. I've created the text file called lauren and the file that I'm trying to read my data from is called RentalCar. The specific string that houses all the rental car data is called Ultimate. Here is what I have so far.


    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class RentalCarReadFromFileWithScanner{
    
    public static void main(String[]args){
    
    File Lauren = new File("C:\\Lauren.txt");
    try{
    Scanner scn = new Scanner(Lauren);
    while(scn.hasNext()){String line = scn.nextLine();
    System.out.println(line);
    }
    
    	}
    	catch(FileNotFoundException fnfe){
    	System.out.println("File Doesn't Exist!!!");	
    		
    	}
    	
    	Formatter output = new Formatter();
    	try{
    	output= new Formatter("C:\\Lauren.txt");
    	 output.format("Lauren");
    	 output.format("\nJohn Doe");
    	output.close();
    }
    
    	catch(FileNotFoundException fnfe){
    		output.close();
    		System.out.println("File Doesn't Exist !!!");
    	}
    	
    	
    	finally {
    		output.close();
    	
    }
    	
    
    	
    
    }
    
    }
    Last edited by Eranga; 04-15-2010 at 03:01 AM. Reason: code tags added

  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
    19

    Default

    Ok, what happen then? Did you comes with any error, better to provide more information about that.

    And also please use code tags next time when you post code segments here in the forum. Unformated codes are really hard to read.

  3. #3
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    I opened the lauren.txt file but nothing came up. I'm trying to have it say what info the user puts in from the joptionpane dialog box from my RentalCar class.

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

    Default

    Your reading file part is okay. Do you want to wrote something from a different class? But I cannot see that passing values to the class you read the file. And also I cannot see any data writing to the file.

  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
    19

    Default

    One bad things happen in your code. Once after the read the content you write some data, actually a line of text and it override the existing content of the file. Means that you've lost all existing data.

Similar Threads

  1. [SOLVED] Read/Write to same XML file
    By Arsenic in forum XML
    Replies: 2
    Last Post: 05-12-2009, 12:51 AM
  2. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  3. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 12:11 PM
  4. File read/write problems
    By p900128 in forum New To Java
    Replies: 4
    Last Post: 06-27-2008, 12:15 AM
  5. Replies: 0
    Last Post: 04-17-2008, 07:36 PM

Posting Permissions

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