Results 1 to 8 of 8
Thread: writing strings to a file
- 09-20-2012, 06:31 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
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:
and heres where the writing is doneJava 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 driverJava 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(); } } } }
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 :)Java Code:public class MenuDriver{ public static void main(String[] args) { MenuClass run = new MenuClass(); run.mainMenu(); } }Last edited by Johnny2009; 09-20-2012 at 07:42 AM.
- 09-20-2012, 06:32 AM #2
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
Re: writing strings to a file
weird sorry for the large breaks
- 09-20-2012, 06:45 AM #3
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
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.
- 09-20-2012, 07:41 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
Re: writing strings to a file
im trying to get the strings from void enterData() to write to a text file in writefile class
- 09-20-2012, 10:39 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: writing strings to a file
This is your save method on the MenuClass:
and this is what the write method on WriteFile looks like:Java Code:public void save() { WriteFile file = new WriteFile(); file.write(); mainMenu(); }
As Zyril says, you are creating a new MenuClass object in that write method.Java Code:public void write(){ MenuClass run = new MenuClass(); FileWriter fWrite = null; BufferedWriter bWrite = null; ... etc etc }
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.
- 09-20-2012, 09:48 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
Re: writing strings to a file
I see. How would i go about that? sorry just not too sure.
- 09-20-2012, 10:41 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 22
- Rep Power
- 0
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 10:44 PM.
- 09-21-2012, 09:36 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.
Similar Threads
-
Problem with File I/O, writing information to a binary file.
By Trithan in forum New To JavaReplies: 1Last Post: 08-02-2012, 03:01 AM -
Problem writing multiple strings to a text file
By Yogesh_P in forum New To JavaReplies: 4Last Post: 03-30-2011, 12:58 AM -
Im writing to a file and i want to skip lines while writing to a text file.
By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 01:29 AM -
Need help writing program to compare 2 strings using a loop
By kornwheat in forum New To JavaReplies: 15Last Post: 11-06-2009, 10:31 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks