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:
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
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
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 :)
Re: writing strings to a file
weird sorry for the large breaks
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.
Re: writing strings to a file
im trying to get the strings from void enterData() to write to a text file in writefile class
Re: writing strings to a file
This is your save method on the MenuClass:
Code:
public void save()
{
WriteFile file = new WriteFile();
file.write();
mainMenu();
}
and this is what the write method on WriteFile looks like:
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.
Re: writing strings to a file
I see. How would i go about that? sorry just not too sure.
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.
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.
Code:
public void write(MenuClass run) {
etc etc
}