0

I'm new to programming and logical errors; I've looked for a similar problem but nothing quite matches mine. We have an assignment to make a program where the user enters their first and last name--if the names are the same, it returns "your first and last name are the same" and if the names are different, it returns "your first and last name are different".

My problem is that it keeps returning BOTH answers no matter what names are typed in. If you run it and try your own name in it, you'll see what I mean.

Here's my code (we have to use scanner):

import java.util.Scanner;

public class NameAssignment
{
    public static void main(String[] args)
    {
    Scanner stdIn = new Scanner(System.in);
    String firstName;
    System.out.print("Enter your first name: ");
    firstName = stdIn.nextLine();
    String lastName;
    System.out.print("Enter your last name: ");
    lastName = stdIn.nextLine();    
    if (firstName == lastName); 
         System.out.println("Hello " + firstName + " " + lastName + "," + " " + "your first name and last name are the same.");
    if (firstName != lastName);
        System.out.println("Hello " + firstName + " " + lastName + "," + " " + "your first name and last name are different.");


} // end main
} // end class NameAssignment

UPDATE

I tried correcting it with the suggestions below, and here's what it looks like now:

import java.util.Scanner;

public class NameAssignment
{
 public static void main(String[] args)
 {
 Scanner stdIn = new Scanner(System.in);
 String firstName;
 System.out.print("Enter your first name: ");
 firstName = stdIn.nextLine();
 String lastName;
 System.out.print("Enter your last name: ");
 lastName = stdIn.nextLine(); 
 if (firstName.equals(lastName)) 
   System.out.println("Hello " + firstName + " " + lastName + "," + " " + "your first name and last name are the same.");
 else (!firstName.equals(lastName)
  System.out.println("Hello " + firstName + " " + lastName + "," + " " + "your first name and last name are different.");
 

} // end main
} // end class NameAssignment

I ran it in terminal, and it gave me this:

NameAssignment.java:16: error: ')' expected
 else (!firstName.equals(lastName)
                                  ^
NameAssignment.java:16: error: not a statement
 else (!firstName.equals(lastName)
      ^
NameAssignment.java:17: error: illegal start of expression
  System.out.println("Hello " + firstName + " " + lastName + "," + " " + "your first name and last name are different.");
        ^
NameAssignment.java:17: error: ';' expected
  System.out.println("Hello " + firstName + " " + lastName + "," + " " + "your first name and last name are different.");
            ^
4 errors
  • 1
    [Take a look at this](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Titus Sep 05 '15 at 13:22
  • 4
    Look at those 'if' statements. What do those trailing semicolons do? – Tony Ennis Sep 05 '15 at 13:23
  • Don't see any JavaScript here. – Felix Kling Sep 05 '15 at 13:24
  • I hope you're using a decent IDE, it should show you where your syntax error is. If you want to use else branch with condition, use `else if` instead of `else`. Also you're missing a closing `)`. That's why it's better to use `{` and `}` around if-else or loop bodies even if they have only one line -- you would get a better error message. – Mifeet Sep 05 '15 at 14:00

3 Answers3

0

To compare 2 Strings in java, you must use if (firstName.equals(lastName)) because in Java == operator checks 2 String objects and .equals() checks String values.

Also, you must remove the semicolon ; after the if statement or else that wont execute your if condition.

burglarhobbit
  • 1,860
  • 2
  • 17
  • 32
0

Your else clause is wrong. Just replace your else clause with the following else clause:

else
    System.out.println("Hello " + firstName + " " + lastName + "," + " " + "your first name and last name are different.");

That's it. else doesn't need a condition. That's why else (!firstName.equals(lastName)) is wrong.

Wololo
  • 841
  • 8
  • 20
  • This actually was the problem! Thank you for your help! I had to move some semicolons around to make it work, but it finally worked correctly. – MiseryMalicious Sep 05 '15 at 14:10
0

you were missing the ) which i added and removed the "," + " " because it was un-efficient and it should just be included in the same string which i fixed. Just replace your code with that and it should work

    if (firstName.equals(lastName)){ 
                 System.out.println("Hello " + firstName + " " + lastName + ", your first name and last name are the same.");
            }
else (!firstName.equals(lastName)){
                System.out.println("Hello " + firstName + " " + lastName + ", your first name and last name are different.");
    }
mateos
  • 1,405
  • 1
  • 17
  • 26