Home arrow static arrow Java Programming [Archive] - Java Problem
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Java Problem
This topic has 3 replies on 1 page.

Posts:49
Registered: 6/17/04
Java Problem  
Jun 17, 2004 6:44 AM



 
I m reletively new to Java Programming and have an Object-Pascal bacground I was designing a User Login form when I encountered a problem that my program wouldn't write to disk I don't know Why? here is the code of the class and the problem section please guide me through...................

class User implements Serializable{
private String UserName;
private String Password;

public User(String usr, String pwd){
UserName = usr;
Password = pwd;
}
public User(){
}

public User(User usr){
this.UserName = usr.UserName;
this.setPassWord(usr.Password);
}

public String getUserName(){
return UserName;
}
public void setPassWord(String pwd){
Password = pwd;
}
public boolean equalto(User usr){
if(this.UserName == usr.UserName)
return true;
else return false;

}
}

The Problem area in my application Login is that it doesn't write the data to disk..........

public boolean isUsr(User usr){
User checkusr = new User(usr);
User usr2 = new User();
try{
FileInputStream fis = new FileInputStream("C:/Program Files/expense.users");
ObjectInputStream in = new ObjectInputStream(fis);

usr2 = (User)in.readObject();
in.close();
}
catch(IOException d){
JOptionPane.showMessageDialog(LoginExpense.this, d.toString()
,"Error", JOptionPane.ERROR_MESSAGE);
}


catch(Exception d){JOptionPane.showMessageDialog(LoginExpense.this, "Some other reasons effected it"
,"Error", JOptionPane.ERROR_MESSAGE);

}

if (usr2.equalto(checkusr))
return true;
else
return false;

}
FileOutputStream fos = new FileOutputStream("C:/Program Files/expense.users", true);
ObjectOutputStream out = new ObjectOutputStream(fos);
if(!isUsr(user)){
out.writeObject(user);
out.flush();
out.close();
JOptionPane.showMessageDialog(LoginExpense.this, "User Created", "Success",
JOptionPane.PLAIN_MESSAGE);
userName.setText("");
passWord.setText("");
}
else
JOptionPane.showMessageDialog(LoginExpense.this, "User already exists", "Failure",
JOptionPane.PLAIN_MESSAGE);
out.close();
}
catch(IOException f){
JOptionPane.showMessageDialog(LoginExpense.this, "Cannot create a new User"
,"Error", JOptionPane.ERROR_MESSAGE);

}
catch(Exception f){
JOptionPane.showMessageDialog(LoginExpense.this, "Cannot create a new User"
,"Error1", JOptionPane.ERROR_MESSAGE);

}
}

What should I do..........................

 

Posts:2,909
Registered: 13.8.2003
Re: Java Problem  
Jun 17, 2004 6:51 AM (reply 1 of 3)



 
Any exceptions? Other information we might be able to use?
Click on Formatting Help and use the code tags to post your code.
 

Posts:118
Registered: 12/19/03
Re: Java Problem  
Jun 17, 2004 7:12 AM (reply 2 of 3)



 
Hi!
First i see the next line of code :

if(this.UserName == usr.UserName) 


I think you need to compare the values of this strings, but you compare here his links. You may never get a true.
So, you need to use the next method (equals):

if(this.UserName.equals(usr.UserName)) 

Try this. If after that you have a problem, paste your code with tag code.
 

Posts:6,487
Registered: 5/5/04
Re: Java Problem  
Jun 17, 2004 8:29 AM (reply 3 of 3)



 
When you get an exception I would printout the WHOLE exception.
If you just display "Error", you are only making very hard for yourself.
 
This topic has 3 replies on 1 page.