Hi,
I'm having loads of trouble writing objects to a file.
Here's my code:
-------------------Menu.java----------------------------
/**
/@Author: Joe Wardell
/@Date: 11/11/02
/@Version: Derek's Dating Agency
*/
import java.io.*;
public class Menu implements Serializable
{
public BufferedReader input = new BufferedReader(
new InputStreamReader(System.in));
public ClientList tempArray = new ClientList(50);
public File path = new File("temp.txt"
public void mainMenu() throws IOException, ClassNotFoundException
{
tempArray.readIn(tempArray);
char option = ' ';
do
{
System.out.println("\n\tWelcome to Derek's Dating Agency\n"
+ "Clients: " + tempArray.size() + "\n"
System.out.println("1: New Client\n"
System.out.println("2: Find Client\n"
System.out.println("3: Show all Data\n"
System.out.println("4: Exit"
clear(3);
System.out.print("Enter option number: "
option = input.readLine().charAt(0);
clear(50);
switch (option)
{
case '1':
addClientMenu();
break;
case '2':
search();
break;
case '3':
showAll();
break;
case '4':
break;
default:
System.out.println("Sorry, " + option
+ " is an invalid option"
}
} while (option != '4');
}
public void showAll()
{
}
public void search() throws IOException
{
String search;
System.out.print("Surname of Customer to Search for: "
search = (input.readLine());
clear(50);
tempArray.searchBySurname(search, array);
}
public void addClientMenu() throws IOException
{
System.out.println("\n\t********** "
+ "Add a New Client"
+ " **********\n"
System.out.print("Forename: "
String firstName = input.readLine();
System.out.print("Surname: "
String lastName = input.readLine();
Client clientTemp = new Client(firstName, lastName);
System.out.print("Sex (m/f): "
char gender;
do
{
gender = (input.readLine().toLowerCase().charAt(0));
switch (gender)
{
case 'm':
clientTemp.setSex(0);
break;
case 'f':
clientTemp.setSex(1);
break;
default:
System.out.println("\t\tSorry, invalid entry. "
+ "Enter either M or F"
}
} while (clientTemp.getSex().equalsIgnoreCase(" ");
System.out.print("Height: "
clientTemp.setHeight(Double.parseDouble(input.readLine()));
System.out.print("Age: "
clientTemp.setAge(Integer.parseInt(input.readLine()));
residenceMenu(clientTemp);
System.out.print("Description of yourself: "
clientTemp.setDescription(input.readLine());
System.out.print("Phone Number: "
clientTemp.setPhone(input.readLine());
System.out.print("E-mail: "
clientTemp.setEmail(input.readLine());
vacationMenu(clientTemp);
clientTemp.setIsObject(true);
tempArray.addClient(clientTemp, array);
return;
}
public void vacationMenu(Client Vclient) throws IOException
{
Vacation vacationTemp = new Vacation(" ", 0, 0, 0, 0);
System.out.print("Destination: "
vacationTemp.setArea(input.readLine());
int holidayType = 0;
do
{
System.out.print("Holiday Types: "
System.out.println("1. Beach?"
System.out.println("\t\t2. Diving?"
System.out.println("\t\t3. Climbing?"
System.out.println("\t\t4. Safari?"
System.out.println("\t\t5. Hiking?"
System.out.print("Option Number: "
holidayType = Integer.parseInt(input.readLine());
}while (holidayType <= 0 | holidayType > 5);
holidayType--;
vacationTemp.setHolType(holidayType);
System.out.print("Minimum cost in pounds: "
vacationTemp.setMinCost(Integer.parseInt(input.readLine()));
System.out.print("Maximum cost in pounds: "
vacationTemp.setMaxCost(Integer.parseInt(input.readLine()));
int when = 0;
do
{
System.out.print("Season: "
System.out.println("\t1. Spring?"
System.out.println("\t\t2. Summer?"
System.out.println("\t\t3. Autumn?"
System.out.println("\t\t4. Winter?"
System.out.print("Option Number: "
when = (Integer.parseInt(input.readLine()));
}while (when <= 0 | when > 4);
when--;
vacationTemp.setTimeOfYear(when);
Vclient.setDestination(vacationTemp);
return;
}
public void residenceMenu(Client Rclient) throws IOException
{
System.out.print("House Name or Number: "
String houseNameOrNum = input.readLine();
System.out.print("Street Name: "
String streetName = input.readLine();
System.out.print("Town: "
String townName = input.readLine();
System.out.print("County: "
String countyName = input.readLine();
System.out.print("Post Code: "
String Pcode = input.readLine();
Residence locationTemp = new Residence(houseNameOrNum,
streetName,
townName,
countyName,
Pcode);
Rclient.setAddress(locationTemp);
return;
}
public void clear(int newLines)
{
for (int i = 0; i <= newLines; i++)
{
System.out.println();
}
}
public static void main (String[] args) throws IOException, ClassNotFoundException
{
Menu test = new Menu();
test.mainMenu();
}
}
----------------------------------------------------------
All the other classes have 'implements Serializable' in the class definition.
The thing is, all the classes compile, then when it actually tries to write to a file I get this command prompt output:
-----------command prompt output from Menu.java-----------
Writing to disk....Exception in thread "main" java.io.NotSerializableException: java.io.Bu
fferedReader
at java.ibjectOutputStream.writeObject0(ObjectOutputStream.java:1054)
at java.ibjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1330)
at java.ibjectOutputStream.writeSerialData(ObjectOutputStream.java:1302)
at java.ibjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1245)
at java.ibjectOutputStream.writeObject0(ObjectOutputStream.java:1052)
at java.ibjectOutputStream.writeObject(ObjectOutputStream.java:278)
at Menu.writeIt(Menu.java:132)
at Menu.addClientMenu(Menu.java:121)
at Menu.mainMenu(Menu.java:42)
at Menu.main(Menu.java:161)
C:\Java\CompSci\Project1>
-----------------------------------------------------------
The thing I don't understand, is hy is it bothered about BufferedReader? that has nothing to do with writing a file!
I know it will be an attribute of any Menu instance, but why does it not like it? an how else could I make a object to take input?
Please, if anyone could giv me a hand I'd be so grateful, I just CANNOT get this to work, and I'm beginning to lose faith in Java :O
=) allllll comments and suggestions are damn welcome!
Trinculo we are all of us living in the gutter.
But some of us are looking at the stars.
I'm having loads of trouble writing objects to a file.
Here's my code:
-------------------Menu.java----------------------------
/**
/@Author: Joe Wardell
/@Date: 11/11/02
/@Version: Derek's Dating Agency
*/
import java.io.*;
public class Menu implements Serializable
{
public BufferedReader input = new BufferedReader(
new InputStreamReader(System.in));
public ClientList tempArray = new ClientList(50);
public File path = new File("temp.txt"
public void mainMenu() throws IOException, ClassNotFoundException
{
tempArray.readIn(tempArray);
char option = ' ';
do
{
System.out.println("\n\tWelcome to Derek's Dating Agency\n"
+ "Clients: " + tempArray.size() + "\n"
System.out.println("1: New Client\n"
System.out.println("2: Find Client\n"
System.out.println("3: Show all Data\n"
System.out.println("4: Exit"
clear(3);
System.out.print("Enter option number: "
option = input.readLine().charAt(0);
clear(50);
switch (option)
{
case '1':
addClientMenu();
break;
case '2':
search();
break;
case '3':
showAll();
break;
case '4':
break;
default:
System.out.println("Sorry, " + option
+ " is an invalid option"
}
} while (option != '4');
}
public void showAll()
{
}
public void search() throws IOException
{
String search;
System.out.print("Surname of Customer to Search for: "
search = (input.readLine());
clear(50);
tempArray.searchBySurname(search, array);
}
public void addClientMenu() throws IOException
{
System.out.println("\n\t********** "
+ "Add a New Client"
+ " **********\n"
System.out.print("Forename: "
String firstName = input.readLine();
System.out.print("Surname: "
String lastName = input.readLine();
Client clientTemp = new Client(firstName, lastName);
System.out.print("Sex (m/f): "
char gender;
do
{
gender = (input.readLine().toLowerCase().charAt(0));
switch (gender)
{
case 'm':
clientTemp.setSex(0);
break;
case 'f':
clientTemp.setSex(1);
break;
default:
System.out.println("\t\tSorry, invalid entry. "
+ "Enter either M or F"
}
} while (clientTemp.getSex().equalsIgnoreCase(" ");
System.out.print("Height: "
clientTemp.setHeight(Double.parseDouble(input.readLine()));
System.out.print("Age: "
clientTemp.setAge(Integer.parseInt(input.readLine()));
residenceMenu(clientTemp);
System.out.print("Description of yourself: "
clientTemp.setDescription(input.readLine());
System.out.print("Phone Number: "
clientTemp.setPhone(input.readLine());
System.out.print("E-mail: "
clientTemp.setEmail(input.readLine());
vacationMenu(clientTemp);
clientTemp.setIsObject(true);
tempArray.addClient(clientTemp, array);
return;
}
public void vacationMenu(Client Vclient) throws IOException
{
Vacation vacationTemp = new Vacation(" ", 0, 0, 0, 0);
System.out.print("Destination: "
vacationTemp.setArea(input.readLine());
int holidayType = 0;
do
{
System.out.print("Holiday Types: "
System.out.println("1. Beach?"
System.out.println("\t\t2. Diving?"
System.out.println("\t\t3. Climbing?"
System.out.println("\t\t4. Safari?"
System.out.println("\t\t5. Hiking?"
System.out.print("Option Number: "
holidayType = Integer.parseInt(input.readLine());
}while (holidayType <= 0 | holidayType > 5);
holidayType--;
vacationTemp.setHolType(holidayType);
System.out.print("Minimum cost in pounds: "
vacationTemp.setMinCost(Integer.parseInt(input.readLine()));
System.out.print("Maximum cost in pounds: "
vacationTemp.setMaxCost(Integer.parseInt(input.readLine()));
int when = 0;
do
{
System.out.print("Season: "
System.out.println("\t1. Spring?"
System.out.println("\t\t2. Summer?"
System.out.println("\t\t3. Autumn?"
System.out.println("\t\t4. Winter?"
System.out.print("Option Number: "
when = (Integer.parseInt(input.readLine()));
}while (when <= 0 | when > 4);
when--;
vacationTemp.setTimeOfYear(when);
Vclient.setDestination(vacationTemp);
return;
}
public void residenceMenu(Client Rclient) throws IOException
{
System.out.print("House Name or Number: "
String houseNameOrNum = input.readLine();
System.out.print("Street Name: "
String streetName = input.readLine();
System.out.print("Town: "
String townName = input.readLine();
System.out.print("County: "
String countyName = input.readLine();
System.out.print("Post Code: "
String Pcode = input.readLine();
Residence locationTemp = new Residence(houseNameOrNum,
streetName,
townName,
countyName,
Pcode);
Rclient.setAddress(locationTemp);
return;
}
public void clear(int newLines)
{
for (int i = 0; i <= newLines; i++)
{
System.out.println();
}
}
public static void main (String[] args) throws IOException, ClassNotFoundException
{
Menu test = new Menu();
test.mainMenu();
}
}
----------------------------------------------------------
All the other classes have 'implements Serializable' in the class definition.
The thing is, all the classes compile, then when it actually tries to write to a file I get this command prompt output:
-----------command prompt output from Menu.java-----------
Writing to disk....Exception in thread "main" java.io.NotSerializableException: java.io.Bu
fferedReader
at java.ibjectOutputStream.writeObject0(ObjectOutputStream.java:1054)
at java.ibjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1330)
at java.ibjectOutputStream.writeSerialData(ObjectOutputStream.java:1302)
at java.ibjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1245)
at java.ibjectOutputStream.writeObject0(ObjectOutputStream.java:1052)
at java.ibjectOutputStream.writeObject(ObjectOutputStream.java:278)
at Menu.writeIt(Menu.java:132)
at Menu.addClientMenu(Menu.java:121)
at Menu.mainMenu(Menu.java:42)
at Menu.main(Menu.java:161)
C:\Java\CompSci\Project1>
-----------------------------------------------------------
The thing I don't understand, is hy is it bothered about BufferedReader? that has nothing to do with writing a file!
I know it will be an attribute of any Menu instance, but why does it not like it? an how else could I make a object to take input?
Please, if anyone could giv me a hand I'd be so grateful, I just CANNOT get this to work, and I'm beginning to lose faith in Java :O
=) allllll comments and suggestions are damn welcome!
Trinculo we are all of us living in the gutter.
But some of us are looking at the stars.