ClassCastExceptions....
Hi folks.
I'm trying to write a class that allows an administrator to add users to given application. This class has an instance of User class, which has the usual sets and gets etc.
My problem arises in the UserList class (which will be pasted below.) I've manged to write to a file with out any trouble at all, however I want the program to load the file up on start up, so rather than re-write the file each time, the program will add users to the file.
My intention is to load the file as an object, and this put it all in a vector. However the read code doesn't seem to work.
The program as it is will compile, I've added the user class to the bottom of it as well, as those are the only two classes that are needed.
Any help would be greatfully recieved
UserList.java
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collections;
import java.util.Vector;
/**
* Class - UserList
*
* @see ToDoList class
*
* This class is loosly based on the to do list class.
*
* @author Emyr Williams
*
* Code HistoryVersion Number
* ====================================================
* 23/10/06 - Date of Birth of code0.0.1a*
* 24/10/06 - File I/O functions added0.0.1b*
* ====================================================
*/
publicclass UserListimplements Serializable
{
User user =null;
Vector<User> users =new Vector<User>();
int index = 0;
String userName;
String passwd;
boolean isAdmin =false;
/**
* Method - addUser
*
* Will add a user to the user list, and then the new user will
* be written to the file soon as the administrator clicks on
* apply.
*/
publicvoid addUser()throws IOException
{
for(;;)
{
user = readUser();
if(user ==null)
{
break;
}
users.add(user);
}
}
// reads the user input from the keyboard.
public User readUser()throws IOException
{
String s =null;
String name =null;
String pass =null;
boolean admin =false;
BufferedReader input =new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please Enter the user name: (! to store the user)");
name = input.readLine().trim();
if(name.charAt(0)=='!')
{
writeToFile();
displayUserList();
returnnull;
}
System.out.println("Please enter the user password: ");
pass = input.readLine().trim();
returnnew User(name, pass);
}
publicvoid displayUserList()
{
System.out.println("User Name \t\t" +"Password");
System.out.println("======================================================");
Collections.sort(users);
for(User user : users)
{
System.out.println(user);
}
}
public String[] toArray()
{
String[] titles =new String[users.size()];
for(int x = 0; x < users.size(); x++)
{
titles[x] = users.get(x).getUserName();
}
return titles;
}
/**
* Method - writeToFile
* @param - Takes a file object as it's parameter so the program
* can know where the file is going to be held.
*
* This method will be used everytime an administrator adds
* a new user to the user list.
*
*/
publicvoid writeToFile()
{
File theFile =new File("users.dat");
ObjectOutputStream objectOut =null;
try
{
objectOut =new ObjectOutputStream(new BufferedOutputStream
(new FileOutputStream(theFile)));
objectOut.writeObject(users);
System.out.println("File written");
}
catch(IOException e)
{
System.err.println(e);
System.exit(0);
}
try
{
objectOut.close();
}
catch(IOException e)
{
e.printStackTrace(System.err);
System.exit(1);
}
}
/**
* Method - loadUserList
*
* This method will be executed when the program starts, and then
* the input from the GUI will be compared with the data in that's
* inputed by the user. Then if the login is successful, the appropriate
* screen will then pop up, and the operator can get to work.
*
* @return - Returns a file (I think)
*/
publicvoid loadUserList()
{
ObjectInputStream objectIn =null;
try
{
objectIn =new ObjectInputStream(new BufferedInputStream
(new FileInputStream("C:/Java/pos/src/users.dat")));
while(true)
{
// I get a ClassCastException here.
// the API tells me that it's something to cast
// an object to a subclass of which it is not an instance
// I'm not sure how I've done this.
user = (User)objectIn.readObject();
System.out.println(user);
// add the contents of the file to the users vector
}
}
catch(EOFException e)
{
e.printStackTrace(System.err);
}
catch(ClassNotFoundException ef)
{
ef.printStackTrace(System.err);
}
catch(IOException eof)
{
eof.printStackTrace(System.err);
}
try
{
objectIn.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
publicvoid clearScreen()
{
for(int i = 0; i < 5; i++)
{
System.out.println("");
}
}
publicstaticvoid main(String [] args)throws IOException, ClassNotFoundException
{
UserList list1 =new UserList();
list1.loadUserList();
list1.displayUserList();
list1.clearScreen();
list1.addUser();
}
}
And here is the User Class.
import java.io.Serializable;
import java.util.Vector;
/**
* Class - User
*Written to hold user details on a file
*that's encrypted (I hope)
* @author Emyr Williams
*
* version 1.0.0 - Written on 29/09/06
*
*/
publicclass Userimplements Comparable<User>, Serializable
{
public String userName;
public String userPassword;
// a check to see if the user is an administrator
publicboolean isAdministrator =false;
/*
* Constructor User
*
* Allows me to get a handle on the user.
*/
public User(String userName, String userPassword,boolean isAdministrator)
{
this.userName = userName;
this.userPassword = userPassword;
this.isAdministrator = isAdministrator;
}
public User(String userName, String userPassword)
{
this.userName = userName;
this.userPassword = userPassword;
}
/*
* The sets
*/
publicvoid setUserName(String theUserName)
{
userName = theUserName;
}
publicvoid setUserPassword(String theUserPassword)
{
userPassword = theUserPassword;
}
/*
* The gets
*/
public String getUserName()
{
return userName;
}
public String getUserPassword()
{
return userPassword;
}
publicint compareTo(User user)
{
int result = userName.compareTo(user.userName);
// TODO Auto-generated method stub
return result;
}
}

