1
package testing;

import java.util.Scanner;

public class Example1 {

    public static void main (String[] args){


    String input; 
    String userInput; 
    String Joshua; 

    Scanner keyboard = new Scanner(System.in);

        System.out.println("What is your name?");
        input = keyboard.nextLine();

        if (input != Joshua){
            System.out.println("You aren't Joshua. Leave.");
        }  
    }

}

//I am receiving the error in the "if" statement. What am I doing wrong? What do I have to do to initialise "Joshua"?

Amit.rk3
  • 2,417
  • 2
  • 10
  • 16
Jodema
  • 19
  • 2
  • 2
    You need to initialize the attribute `Joshua`(e.g. `String Joshua = "Joshua";`). Also, [never compare Strings with `==` or `!=`](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Turing85 Jul 11 '15 at 20:47

3 Answers3

3
String joshua = "Joshua"; 

You just named your variable, but did not assign any value.

Additionally, use the equals(Object obj) method to compare strings! The == operator only checks, whether the two variables share the same location in the memory, the equals() method checks, if the content is equal.

if(joshua.equals(input))
{
// just do it
}
kevcodez
  • 1,261
  • 12
  • 27
  • I'm new to programming so the equals(Object obj) method is nigh inscrutable. Thanks for the lesson. – Jodema Jul 13 '15 at 00:13
0

You should do

if (input.equals("Joshua") {
    //Master stuff...
} else {
    //You are not Joshua!
}

Okay, so

1: You never defined a value for the string Joshua.

2: You don't even need a variable for that. Just do "Joshua" in the if statement.

EDIT: Okay, if you really wanted to use a variable for that, you initialize it by doing:

String Joshua = "Joshua";

And when it comes to strings, use .equals() or .equalsIgnoreCase() instead of == or =! to compare strings!

Lukas Wiklund
  • 826
  • 6
  • 21
  • Okay. Thank you. Now my code isn't even acknowledging my if statement at all. There is no error done by the compiler: – Jodema Jul 11 '15 at 22:10
  • System.out.println("What is your name?"); input = keyboard.nextLine(); if (input == Joshua){ System.out.println("Hello, master. How are you feeling today?"); if (input != Joshua){ System.out.println("You aren't Joshua. Try again."); input = keyboard.nextLine(); – Jodema Jul 11 '15 at 22:11
  • I updated the code. Check it, and don't forget to do initialize the variable as I showed you if you want to use it. – Lukas Wiklund Jul 11 '15 at 22:26
0

In java instance variables (defined in class) get their default values.

You have initialize to Local variables (defined in methods). Because they don't have any default value.

  1. You can use String Joshua = null; or String Joshua = "some value here"; to initialize Joshua variable and then use.

  2. You can make your variable an instance (define Joshua in class) and make it static to use directly in your static method main.

One more thing, you are not following Java naming convention for your Joshua variable. It should be joshua.

Hope this help :)

Hemant Sharma
  • 1,239
  • 12
  • 9