-1

I need help with my Java code. For some reason it skips the System.out.println("Username does not exist");. I'm almost sure i messed up with the {} somewhere. Help would be appreciated.

Updated:

import java.util.Scanner;

class Bot {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Do you want to start the bot?");
    String startup = input.next();
    if (startup.equalsIgnoreCase("yes") || startup.equalsIgnoreCase("y")) {
        System.out.println("Okay, starting...");
        System.out.println("Please type your username");
        String user = input.next();
        if (user.equalsIgnoreCase("admin")) {
            System.out.println("Valid username. Please type your password.");
            String pass = input.next();
            if (pass.equalsIgnoreCase("admin") || pass.equalsIgnoreCase("root")) {
                System.out.println("Logging in...");
            } else if (!(pass.equalsIgnoreCase("admin") || pass.equalsIgnoreCase("root"))) {
                System.out.println("Wrong password.");
            } else {
                System.out.println("Username does not exist");
            }
            ;
        }
    } else if (startup.equalsIgnoreCase("no") || startup.equalsIgnoreCase("n")) {
        System.out.println("Okay, shutting down...");
    } else {
        System.out.println("Please answer with 'yes', 'y', 'no' or 'n', not '" + startup + "'.");
    }
}
};

Original:

class Bot {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Do you want to start the bot?");
        String startup = input.next();
        if (startup.equalsIgnoreCase("yes") || startup.equalsIgnoreCase("y")) {
            System.out.println("Okay, starting...");
            System.out.println("Please type your username");
            String user = input.next();
            if (user.equalsIgnoreCase("admin")) {
                System.out.println("Valid username. Please type your password.");
                String pass = input.next();
                if (pass.equalsIgnoreCase("admin") || pass.equalsIgnoreCase("root")) {
                    System.out.println("Logging in...");
                } else if (!(pass == "admin")) {
                    System.out.println("Wrong password.");
                } else if (!(user == "admin")) {
                    System.out.println("Username does not exist");
                }
                ;
            }
        } else if (startup.equalsIgnoreCase("no") || startup.equalsIgnoreCase("n")) {
            System.out.println("Okay, shutting down...");
        } else {
            System.out.println("Please answer with 'yes', 'y', 'no' or 'n', not '" + startup + "'.");
        }
    }
    };
Vena
  • 35
  • 5

1 Answers1

0
} else if(!(user=="admin"))

You can't compare strings with ==; you have to do it in a similar way to how you did it further up: if(!user.equalsIgnoreCase("admin")) {.

In this case a simple } else { would work however.

Androidas
  • 169
  • 10