This discussion is archived
2 Replies Latest reply: Nov 11, 2012 11:48 PM by 973712 RSS

Problem in reading text by JOptionPane Inputbox..

973712 Newbie
Currently Being Moderated
I am very new to java programming and i am learning java by my self.saw i have started learning and while learning i got this error.Please help me I really want to know whats behind this.

I have planned to create simple login by my simple knowledge.saw i used this codes to do it.It has no errors on cording saw it compiled.but when i run program and write something on input box it always shows else out put(i meen "Username and Password Incorrect")from my code.If i write username and password correct also it shows same output.what is the wrong i have done on this code.Plz help me?

import javax.swing.*;
class Login
{
     public static void main(String[]args)
     {
     String username=JOptionPane.showInputDialog("Enter Username :");
     String password=JOptionPane.showInputDialog("Enter Password :");
     //String lasanda="lasanda";
     //String first="first";
          if(username=="lasanda" && password=="first")
          {
          JOptionPane.showMessageDialog(null,"You have logged in to system Succesfully!");
          }
          else if(username!="lasanda" && password=="first")
          {
          JOptionPane.showMessageDialog(null,"Username Incorrect");
          }
          else if(username=="lasanda" && password!="first")
          {
          JOptionPane.showMessageDialog(null,"Password Incorrect");
          }
          else
          {
          JOptionPane.showMessageDialog(null,"Username and Password Incorrect");
          }
     }
}
  • 1. Re: Problem in reading text by JOptionPane Inputbox..
    Umer Journeyer
    Currently Being Moderated
    Hi,

    use the following code and find the difference with yours.
    import javax.swing.JOptionPane;
    class Login
    {
        public static void main(String[]args)
        {
            String username = JOptionPane.showInputDialog("Enter Username :");
            String password = JOptionPane.showInputDialog("Enter Password :");
            //String lasanda="lasanda";
            //String first="first";
            if("lasanda".equals(username) && "first".equals(password))
            {
                    JOptionPane.showMessageDialog(null,"You have logged in to system Succesfully!");
            }
            else if(!"lasanda".equals(username) && "first".equals(password))
            {
                    JOptionPane.showMessageDialog(null,"Username Incorrect");
            }
            else if("lasanda".equals(username) && !"first".equals(password))
            {
                    JOptionPane.showMessageDialog(null,"Password Incorrect");
            }
            else
            {
                    JOptionPane.showMessageDialog(null,"Username and Password Incorrect");
            }
        }
    }
    http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals%28java.lang.Object%29
  • 2. Re: Problem in reading text by JOptionPane Inputbox..
    973712 Newbie
    Currently Being Moderated
    thanx for your help.I will try this.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points