Results 1 to 14 of 14
- 04-15-2010, 02:48 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
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
- 04-15-2010, 03:04 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 04-15-2010, 03:08 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
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.
- 04-15-2010, 03:12 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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]); */
- 04-15-2010, 03:15 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
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.
- 04-15-2010, 03:28 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Fine. What you suppose to do with the PrintStream?
- 04-15-2010, 03:34 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
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.
- 04-15-2010, 03:37 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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?
- 04-15-2010, 03:38 AM #9
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
yes that is correct but I am newer to java and I am not quite sure how to do it.
- 04-15-2010, 03:42 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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...");
- 04-15-2010, 03:44 AM #11
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
Yes, I do.
- 04-15-2010, 03:47 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
So then you've to do the necessary validation with the user input, before modify the file.
- 04-16-2010, 01:33 AM #13
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
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
- 04-16-2010, 02:51 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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
-
how to write text on an existing image
By jeshmal4u in forum Java 2DReplies: 6Last Post: 05-07-2010, 06:14 PM -
Write to a txt. file, via user input? (java with netbeans)
By Glypsen in forum NetBeansReplies: 6Last Post: 03-01-2010, 07:00 AM -
called external program does not automatically write file
By nickvandewiele in forum New To JavaReplies: 6Last Post: 02-25-2010, 02:38 PM -
How do I validate user input from forms with Java?
By rickywh in forum New To JavaReplies: 2Last Post: 01-30-2010, 06:49 AM -
reading from input file and then write on it
By sara12345 in forum New To JavaReplies: 9Last Post: 01-19-2010, 11:41 AM
Bookmarks