Results 1 to 8 of 8
  1. #1
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default writing strings to a file

    Hi everyone, so my problem is im trying to call some strings that the user inputs from one class to another class that writes those strings into the file. when i read the file i get null on the strings because im just calling the strings with nothing in them i guess. help please D:

    Java Code:
    import java.util.Scanner ;
    
    public class MenuClass
    {
      static Scanner console = new Scanner(System.in);
      String name, address, city, state, zip, school, phone;
      public MenuClass()
      {
      }
      
      public void mainMenu()
      {
        int numChoice;
        
        System.out.println("     MAIN MENU");
        System.out.println("1. Enter Data");
        System.out.println("2. Display Data");
        System.out.println("3. Edit Data");
        System.out.println("4. Delete Data");
        System.out.println("5. Read File");
        System.out.println("6. Save to File");
        System.out.println("7. Select Number Choice 1-7");
        numChoice = console.nextInt();
       
        
        switch (numChoice){
          case 1: enterData();
          break;
          case 2: display();
          break;
          case 3: edit();
          break;
          case 4: delete();
          break;
          case 5: readFile();
          break;
          case 6: save();
          break;
          default:
            System.out.println("Invalid Choice...");
            System.out.println("RESTARTING...");
            mainMenu();
            break;
        
      }
      }
      public void enterData()
      {
        //String name, address, zip, school, phone;
        System.out.print("Name: ");
        name = console.next();
        System.out.print("Address: ");
        address = console.next();
        System.out.print("Zip: ");
        zip = console.next();
        System.out.print("School: ");
        school = console.next();
        System.out.print("Phone #: ");
        phone = console.next();
        System.out.print("\nSaving...");
        mainMenu();
       
       
      }
      
      public void display()
      {
         int numChoice;
         System.out.println("Name: " + name);
         System.out.println("Address: " + address);
         System.out.println("Zip: " + zip);
         System.out.println("School: " + school);
         System.out.println("Phone #: " + phone);
         System.out.println("    Options");
         System.out.println("1. Return to Main Menu");
         System.out.println("2. Edit Data");
         System.out.print(" Select 1 or 2:");
         numChoice = console.nextInt();
         
         switch(numChoice)
         {
           case 1: mainMenu();
           break;
           case 2: edit();
           break;
           default: System.out.println("Error... Returning to Main");
                    mainMenu();
           break;
         }
           
      }
      public void edit()
      {
         int numChoice;
         System.out.println("Name: " + name);
         System.out.println("New Name: ");
         name = console.next();
         System.out.println("Address: " + address);
         System.out.println("New Address: ");
         address = console.next();
         System.out.println("Zip: " + zip);
         System.out.println("New Zip: ");
         zip = console.next();
         System.out.println("School: " + school);
         System.out.println("New School:");
         school = console.next();
         System.out.println("Phone #: " + phone);
         System.out.println("New Phone #:");
         phone = console.next();
         System.out.println("    Options");
         System.out.println("1. Return to Main Menu");
         System.out.println("2. Edit Data");
         System.out.print(" Select 1 or 2:");
         numChoice = console.nextInt();
         
           switch(numChoice)
         {
           case 1: mainMenu();
           break;
           case 2: edit();
           break;
           default: System.out.println("Error... Returning to Main");
                    mainMenu();
           break;
         }
      }
     
      public void menuDisplay()
      {
       
      }
     
      public void delete()
      {
        int numChoice;
        name = "";
        address = "";
        zip = "";
        school = "";
        phone = "";
        System.out.println("Data Cleared");
         System.out.println("    Options");
         System.out.println("1. Return to Main Menu");
         System.out.println("2. Display");
         System.out.println("3. Save");
         System.out.print(" Select 1-3:");
         numChoice = console.nextInt(); 
       
         switch(numChoice)
         {
           case 1: mainMenu();
           break;
           case 2: display();
           break;
           case 3: save();
           break;
           default: System.out.println("Error... Returning to Main");
                    mainMenu();
           break;
         }
      }
      public void readFile()
      {
        WriteFile file = new WriteFile();
        file.read();
      }
      public void save()
      {
        
        WriteFile file = new WriteFile();
        file.write();
        mainMenu();
      }
    }
    and heres where the writing is done

    Java Code:
    import java.io.BufferedWriter;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.FileWriter;
     
     public class WriteFile{
       
       public void write(){
         MenuClass run = new MenuClass();
         FileWriter fWrite = null;
         BufferedWriter bWrite = null;
         
         try {
           fWrite = new FileWriter("PersonalInfo.txt");
           bWrite = new BufferedWriter(fWrite);
           
           bWrite.write("Name: " + run.name);
           bWrite.newLine();
            bWrite.write("Address: " + run.address);
            bWrite.newLine();
             bWrite.write("Zip: " + run.zip);
             bWrite.newLine();
              bWrite.write("School: " + run.school);
              bWrite.newLine();
               bWrite.write("Phone #: " + run.phone);
           bWrite.newLine();
           
           bWrite.close();
         } catch (Exception e) {
         }
       }
       public void read(){
         BufferedReader br = null;
        
        try {
        
         String sCurrentLine;
           
        br = new BufferedReader(new FileReader("personalinfo.txt"));
                
         while ((sCurrentLine = br.readLine()) != null)
        {
         System.out.println(sCurrentLine);
                    }
                        
       } catch (IOException e)
       {
        e.printStackTrace();
                   } finally {
                    try {
         if (br != null)br.close();
                    } catch (IOException ex) {
          ex.printStackTrace();
        }
           }
        
     }
     }
    and driver

    Java Code:
    public class MenuDriver{
      public static void main(String[] args)
      {
        MenuClass run = new MenuClass();
        
        run.mainMenu();
       
      }
    }
    i do need help with some other things like not allowing letters instead of numbers for the choices, and writing multiple sets of data to a file. any help will do thanks :)
    Last edited by Johnny2009; 09-20-2012 at 08:42 AM.

  2. #2
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Re: writing strings to a file

    weird sorry for the large breaks

  3. #3
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: writing strings to a file

    Please, separate your questions and ask them one at the time in the thread. Right now I'm still figuring out what you need help with. The topic says one thing, the text says several other things.

    1. To accept letters instead of numbers for the choices.
    Just do an if statement that uses your switch-codes if the letter equals something? You need to be more specific.

    2. "call some strings that the user inputs from one class to another class that writes those strings into the file".
    I'm not sure you want to call the methods in the String objects. You want to save objects of the String class in a file?
    Do you want to save them as plain text?
    When is your object called "run" receiving what to write? You just create a new object and write the null's?

    Indent your code properly and it will be easier to follow it.

  4. #4
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Re: writing strings to a file

    im trying to get the strings from void enterData() to write to a text file in writefile class

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: writing strings to a file

    This is your save method on the MenuClass:
    Java Code:
    public void save()
    {
       WriteFile file = new WriteFile();
       file.write();
       mainMenu();
    }
    and this is what the write method on WriteFile looks like:
    Java Code:
    public void write(){
       MenuClass run = new MenuClass();
       FileWriter fWrite = null;
       BufferedWriter bWrite = null;
    ... etc etc
    }
    As Zyril says, you are creating a new MenuClass object in that write method.
    It is not the same as the one that has got hold of the information off the user.

    In this situation you would normally pass in the MenuClass object you are actually working on, either in the constructor of WriteFile or in the write() method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Re: writing strings to a file

    I see. How would i go about that? sorry just not too sure.

  7. #7
    Johnny2009 is offline Member
    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    Default Re: writing strings to a file

    This is what I did. I just added an extends MenuClass to WriteFile. And removed the new MenuClass object.

    Im still getting null though.
    Last edited by Johnny2009; 09-20-2012 at 11:44 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: writing strings to a file

    You don't need to extend anything.
    You just pass the object reference into the method.
    You're doing it all the time with calls to things like write() on your BufferedWriter and println().
    You just need to make sure the write() method on WriteFile accepts a MenuClass reference.
    Java Code:
    public void write(MenuClass run) {
    etc etc
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 08-02-2012, 04:01 AM
  2. Problem writing multiple strings to a text file
    By Yogesh_P in forum New To Java
    Replies: 4
    Last Post: 03-30-2011, 01:58 AM
  3. Replies: 1
    Last Post: 02-27-2010, 02:29 AM
  4. Replies: 15
    Last Post: 11-06-2009, 11:31 AM
  5. Replies: 3
    Last Post: 05-10-2009, 12:31 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
  •