0

So I'm having problems with the Scanner class in java. The console output is always the same no matter what my input is. I wanted the program to display the sum (8) when the user types "yes" and end the program when the user types "end". Instead, the program ignores my input and continues to ask for it.

public static void main(String[] args) {
        
    Scanner sc = new Scanner(System.in);
    String choose = "sdjajkk";
    System.out.print("type anything:");
    
    for (int i=0; choose!="end"; i++)
    {
        System.out.println(i);
        choose = sc.nextLine();
        System.out.println("if u typed yes then u will get sum");
        if (choose=="yes")
        {
        int c = 3+5;
        System.out.println(c);
        }
    } sc.close();
    
    }

ThaT was my code^

and here is my console: as you can see scanner does not react to any of the commands

Is there a problem with including the scanner in the for loop? Or does the sc.nextLine() scanner read the wrong line?

kaya3
  • 47,440
  • 4
  • 68
  • 97

0 Answers0