简体   繁体   中英

always execute the last statement in java

when i run the code, its always execute the last statement (admin) although if i enter correct/incorrect userid or password for different role position, only open the admin.java , can't open others statement

Scanner input1 = new Scanner(System.in);
System.out.println("User ID : ");
String salesid = input1.next();
Scanner input2 = new Scanner(System.in);
System.out.println("Password : ");
String salespass = input2.next();

String smposition = "SM";
String pmposition = "PM";
String adposition = "AD";
{
    try
    {
        String line;
        File f = new File("UserDetails.txt");
        input1 = new Scanner(f);
        while(input1.hasNextLine())
        {
            String Line = input1.nextLine();
            String[] Seperator = Line.split(",");

            if(salesid.equals(Seperator[0]) && salespass.equals(Seperator[3]) && smposition.equals(Seperator[2]) || pmposition.equals(Seperator[2]) || adposition.equals(Seperator[2]))
            { 
                if (smposition.equals(Seperator[2]))
                {
                    System.out.println("login successfull");
                    salesmanager ss = new salesmanager();
                    ss.sale();
                }
                else if(pmposition.equals(Seperator[2]))
                {
                    System.out.println("login successfull");
                    purchasemanager pm = new purchasemanager();
                    pm.purchase();
                }
                else if(adposition.equals(Seperator[2]))
                {
                    System.out.println("login successfull");
                    admin ad = new admin();
                    ad.adm();
                }
            }
        }
    }
    catch(IOException e)
    {
    }
}

Change your if condition to

if(salesid.equals(Seperator[0]) && salespass.equals(Seperator[3]) && (smposition.equals(Seperator[2]) || pmposition.equals(Seperator[2]) || adposition.equals(Seperator[2])))

Note the extra parentheses around the || conditions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM