1

I had a similar code to this using numbers, and it worked perfectly. This however keeps underlining the word else and I don't know why. I am just playing around with java trying to understand a few principles.

I want to program to reply one of two statements depending on input. Also, where it says if (input1 == "Hello");, I wanted to put if (input1 == "Hello" || "hello"); to accept lowercase too, but that showed errors too.

Just to be clear, if i remove the else clause, my program runs and both statements are printed!

import java.util.Scanner;

public class Input
{
    public static void main(String[] args) 
    {
    System.out.println("Hello there!");
    Scanner Scan = new Scanner (System.in);
    String input1 = Scan.nextLine();
    Scan.close();

    if (input1 == "Hello");
        {
            System.out.println("How are you?");
        }
        else
            System.out.println("How rude, you didn't even say Hello!");
            break;
        }
    }
}
hexacyanide
  • 88,222
  • 31
  • 159
  • 162
DrKhan
  • 313
  • 1
  • 3
  • 10
  • I'm not seeing this as an *exact* duplicate. The duplicate question could be updated to include an answer in regards to comparisons for lower case values, too. – Makoto Aug 18 '13 at 19:20
  • 2
    You need to look up a few tutorials. You are closing your if statement before if executes, using == instead of .equals() to compare strings and using a break statement despite the fact you aren't using loops. – Andrew Martin Aug 18 '13 at 19:20
  • Thank you! I appreciate the help, I have got it working and learnt something new too. Is it possible for my if statement to use multiple strings using ||. For example: if (input1.equalsIgnoreCase("Hello" || "Hey" || "Hi"), is there a more efficient way rather than creating multiple, else if statments – DrKhan Aug 18 '13 at 19:26
  • 1
    @DrKhan: Yup. That's called the or operator. In your case, it makes more sense to compare using equalsIgnoreCase(), otherwise you would have to change the || everytime you changed the input word that was being compared. – Andrew Martin Aug 18 '13 at 19:27
  • 1
    @DrKhan you can't use `||` inside String.equalsIgnoreCase. To get the result you're going for you'd need to do `if(input1.equalsIgnoreCase("hello") || input1.equalsIgnoreCase("hey") || input1.equalsIgnoreCase("hi")){/*conditions*/}` – Seren Aug 18 '13 at 19:34
  • @DrKhan:- I have updated my answer for your query regarding OR. Check it out :) – Rahul Tripathi Aug 18 '13 at 19:37
  • 1
    I'll just point out that my original "Yup" is now wrong as you've changed the comment I replied to! Follow @seren01's comment instead! – Andrew Martin Aug 18 '13 at 19:46

5 Answers5

5

Never use == to compare strings.

use .equals instead.

if (input1.equals("Hello"))

or

if (input1.equalsIgnoreCase("Hello"))

Delete the semicolon at the end of

if (input1 == "Hello");

EDIT:- As seen in your comments regarding OR.

You can try this:-

if(input1.equalsIgnoreCase("hello") || input1.equalsIgnoreCase("hey") || input1.equalsIgnoreCase("hi"))
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

Remove the ; at the end of your if statement. And use .equals() to compare strings.

The semicolon causes the compile error, while the == will cause a logical error once it does run.

jrtc27
  • 8,496
  • 3
  • 36
  • 68
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • @JoshM: the semicolon causes a compilation error on the `else`. – Jeroen Vannevel Aug 18 '13 at 19:24
  • If we want to bug someone to modify the compiler to give a warning when this type of semicolon error appears, whom should we contact? This question keeps coming up over and over. – ajb Aug 18 '13 at 19:58
1

You probably don't want the semicolon on the line

if (input1 == "Hello");

You also probably do not actually want to compare using == (read the linked question about comparing strings).

Third, why is there a break statement in your else clause?

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
  • 1
    To expand on the third point here (for the OP), break statements are used to exit loops - you don't have a loop, so there's no need for it to be there. – Andrew Martin Aug 18 '13 at 19:19
1

You're looking for equalsIgnoreCase(). This compares two Strings without any regard to case.

Makoto
  • 104,088
  • 27
  • 192
  • 230
0

Remove the ; after your if statement condition and add a { after else. Also, it is good practice to to use the equals(Object) method when comparing objects because you might get unexpected results when using ==.

Josh M
  • 11,611
  • 7
  • 39
  • 49