Java – the scanner did not scan my input

I'm trying to read a line and then enter the if statement But it will terminate after the first input of red

import java.util.Scanner;

public class mainen {

    public static void main(String[] args) {
        Formler form = new Formler();
        Scanner Sscan = new Scanner(system.in);
        Scanner Dscan = new Scanner(system.in);
        String input;
        System.out.println("Formler: Molmassa");
        input = Sscan.nextLine();

        if(input == "molmassa" || input == "Molmassa"){
            double m;
            double M;
            System.out.println("Massa: "); m = Dscan.nextDouble();
            System.out.println("Molmassa: "); M = Dscan.nextDouble();
            System.out.println(form.getMolmassa(m,M));
        }
    }

}

Solution

Change your if statement to:

if(input.equalsIgnoreCase("molmassa") ) {  }

Then it should work as expected Remember, always compare strings using the equals () or equalsignorecase () methods== Compare object references instead of actual values

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>