-3

So I wrote up a code that will make the user enter the name "Jack" but if he enters any other name it will say "That is not your name!" but i'm having trouble to make it do that. For example when I enter that name "Jack" It will say "That is not your name!" and when I enter a name like Joey for example it will say the same thing.

import java.util.Scanner;

public class WhoAreYou
{
  public static void main(String[] args){

    Scanner user_input = new Scanner(System.in);

    String vYourName, vNotYourName;
    System.out.print("Enter your name here: ");
    vYourName = user_input.next();

    /*
    String vNotYourName;
    System.out.print("Enter your name here: ");
    vNotYourName = user_input.next();
    */

    if(vYourName == "Jack")
    System.out.println("Your name is: "+vYourName);

    else{
      vNotYourName = " ";
      System.out.println("Thats not your name!");
    }
  }
}

Instead if using two Strings I tried to use one and that didn't work. Any ideas?

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153

2 Answers2

0

use .equals() when comparing strings:

    if(vYourName.equals("Jack"))
    System.out.println("Your name is: "+vYourName);

    else{
      "".equals(vYourName);
      System.out.println("Thats not your name!");
    }
ltalhouarne
  • 4,586
  • 2
  • 22
  • 31
0

instead of:

if(vYourName == "Jack")

use this:

if(vYourName.equals("Jack"))