Results 1 to 14 of 14
  1. #1
    gmoney8316 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Question How do I write to an existing file in Java with a program that asks for user input?

    I have written a program that reads a file named addressbook.csv. I am having problems with how to ask the user what the lastname is that they would like to change and then making sure that the change stays in the program. I also need to make sure that if the lastname that the user enters is not there, that it tells the user the name does not exist. Can anyone help? Here is the program:

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    
    public class Lab8
    {
    public static void main(String[] args)
    {
    readFileNew();
    }
    
    public static void readFileNew()
    {
    String[] firstName = new String[50];
    String[] lastName = new String[50];
    String[] eMail = new String[50];
    String[] homePhone = new String[50];
    String[] cellPhone = new String[50];
    String[] city = new String[50];
    String[] state = new String[50];
    
    try
    {
    Scanner line = new Scanner(new File("addressBook.csv"));
    int newCounter = 1;
    while(line.hasNextLine())
    {
    String temp = line.nextLine();
    System.out.println(temp);
    Scanner lineScanner = new Scanner(temp);
    lineScanner.useDelimiter(",");
    
    firstName[newCounter] = lineScanner.next();
    lastName[newCounter] = lineScanner.next();
    eMail[newCounter] = lineScanner.next();
    homePhone[newCounter] = lineScanner.next();
    cellPhone[newCounter] = lineScanner.next();
    city[newCounter] = lineScanner.next();
    state[newCounter] = lineScanner.next();
    
    
    /* System.out.println(firstName[newCounter] + " " + lastName[newCounter] + ": " + eMail[newCounter]);
    System.out.println("Home phone number: " + homePhone[newCounter]);
    System.out.println("Cell phone number: " + cellPhone[newCounter]);
    System.out.println("Resides in " + city[newCounter] + ", " + state[newCounter]);
    */ 
    
    PrintStream addressBookOutput = new PrintStream(new File("addressBook.csv"));
    for(int c = 0; c < lastName.length; c++) 
    {
    addressBookOutput.print(lastName[c] + ",");
    if ( c < lastName.length-1 )
    addressBookOutput.println();
    }
    newCounter++;
    }
    }
    catch(Exception exception)
    {
    System.out.println("A problem occurred");
    }
    }
    }
    Last edited by Eranga; 04-15-2010 at 03:04 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

  3. #3
    gmoney8316 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    I am stuck with the Printstream and how to do the JOption to get a response to the file so that it changes the file. I am not sure exactly where to go with it.

  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

    Let see first part of your question. You've done something like this and it's commented out.

    Java Code:
    /* System.out.println(firstName[newCounter] + " " + lastName[newCounter] + ": " + eMail[newCounter]);
    System.out.println("Home phone number: " + homePhone[newCounter]);
    System.out.println("Cell phone number: " + cellPhone[newCounter]);
    System.out.println("Resides in " + city[newCounter] + ", " + state[newCounter]);
    */
    Why is that?

  5. #5
    gmoney8316 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    That was originally the lab before this one but I left it in. I wasn't sure if I still needed it so I commented it out just in case.

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

  7. #7
    gmoney8316 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    I believe I am suppose to use it to ask the user what lastname they want to change and then enter what they want to change it to. It should change the file and save it to the new name. I'm not exactly sure what to do with the printstream. What I have here does compile but I am unsure if it is correct.

  8. #8
    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

    In other words what you want to do is, ask question from the user and write them to the file, after doing your validation and all. Isn't it?

  9. #9
    gmoney8316 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    yes that is correct but I am newer to java and I am not quite sure how to do it.

  10. #10
    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

    Then what you've do is, collect the information from the user. Do you know how to use JOptions input dialogs.

    Java Code:
    String str = JOptionPane.showInputDialog(null, "Your question...");

  11. #11
    gmoney8316 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    Yes, I do.

  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
    19

  13. #13
    gmoney8316 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    Ok, I have figured out most of the program but I am still having problems with the Part 1 of the code:

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    
    public class Lab8
    {
      public static void main(String[] args)
      {
        readFileNew();
      }
      
      public static void readFileNew()
      {
        String[] firstName = new String[50];
        String[] lastName = new String[50];
        String[] eMail = new String[50];
        String[] homePhone = new String[50];
        String[] cellPhone = new String[50];
        String[] city = new String[50];
        String[] state = new String[50];
        
        try
        {
          Scanner line = new Scanner(new File("addressBook.csv"));
          int newCounter = 1;
          while(line.hasNextLine())
          {
            String temp = line.nextLine();
            System.out.println(temp);
            Scanner lineScanner = new Scanner(temp);
            lineScanner.useDelimiter(",");
            
            
            
            firstName[newCounter] = lineScanner.next();
            lastName[newCounter] = lineScanner.next();
            eMail[newCounter] = lineScanner.next();
            homePhone[newCounter] = lineScanner.next();
            cellPhone[newCounter] = lineScanner.next();
            city[newCounter] = lineScanner.next();
            state[newCounter] = lineScanner.next();
            
            newCounter++;
          }
          
          //Part 1
          {
            
            //Part 2
            String str = JOptionPane.showInputDialog(null, "What Last Name would you like to Change?");
            
          }  
    //Part 3
          PrintStream addressBookOutput = new PrintStream(new File("addressBook2.csv"));
          for(int c = 0; c < lastName.length; c++) 
          {
            addressBookOutput.print(lastName[c] + ",");
            if ( c < lastName.length-1 )
              addressBookOutput.println();
          }
          
          
        }
        catch(Exception exception)
        {
          System.out.println("A problem occurred");
          exception.printStackTrace();
        }
        
      }
      
      
    }
    Last edited by Eranga; 04-16-2010 at 02:49 AM. Reason: code tags added

  14. #14
    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

    Please use code tags next time. If you don't know how to do it, look at my signature. You can find a link to the relevant FAQ page.

    Regarding your question. What's the part 1. You have only two things, JOption user information capture and the PrintStream, so what?

Similar Threads

  1. how to write text on an existing image
    By jeshmal4u in forum Java 2D
    Replies: 6
    Last Post: 05-07-2010, 06:14 PM
  2. Replies: 6
    Last Post: 03-01-2010, 07:00 AM
  3. called external program does not automatically write file
    By nickvandewiele in forum New To Java
    Replies: 6
    Last Post: 02-25-2010, 02:38 PM
  4. How do I validate user input from forms with Java?
    By rickywh in forum New To Java
    Replies: 2
    Last Post: 01-30-2010, 06:49 AM
  5. reading from input file and then write on it
    By sara12345 in forum New To Java
    Replies: 9
    Last Post: 01-19-2010, 11:41 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
  •