-3

I'm trying to make it so when the user writes Start the program does something, but I'm unsure of how to what the user actually wrote.

This was my first attempt at it:

import java.util.Scanner;

    public class Suhwag {
        public static void main (String args[]){
            Scanner scanNer = new Scanner (System.in);
            System.out.println("Please write \"Start\" to begin.");
            String stinky = "Start";
            if (stinky == scanNer);

But with this, I got the error message:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Incompatible operand types String and Scanner

After I saw the error, I tried to convert scanNer to a string as seen here:

import java.util.Scanner;

        public class Suhwag {
            public static void main (String args[]){
                Scanner scanNer = new Scanner (System.in);
                System.out.println("Please write \"Start\" to begin.");
                String stinky = "Start";
                String input = scanNer.nextLine();
                if (stinky == scanNer);

But the same error message still appears. Anyone know what I could do to make it work?

moxide
  • 65
  • 6

4 Answers4

0
  1. You are comparing a String to a Scanner object.
  2. You should use the equals method to compare String's
  3. No need for the semi-colon after the if (see below)

In reference to your last code snippet:

if (stinky.equals(input)){
    //do something
}
copeg
  • 8,092
  • 17
  • 27
0

You're trying to compare a Scanner object with a String object. First, you could input the string with the following line:

String myString = scanNer.next()

Then, compare it with "Start":

if ( myString.equals( "Start" ) )
{
    ...
}
0

in the latter code area you said in your if statement:

stinky == scaNer

it should be

stinky.equals(input)
-2

in your if statement, you compared still your scanner with ur stinky

change your if statment to this

if (input.equals(stinky)){<code here>}

your previous code didnt work because you compare a scanner with a string

enter image description here

Mazino
  • 542
  • 5
  • 21