Python - how to use simple if statement

So i wanted to create a simple if statement in which if the name imputed is “jose”, the true would print (hello jose) and if it was false, it would say “hello stranger” pretty straightforward, but when i try to do that, it runs just fine with no errors, but no matter what i type in as the name… it prints the false statement

package test;
import java.util.Scanner;
public class testingif {
public static void main(String\[\] args) {
	Scanner scan = new Scanner(;
	String name;
	System.out.println("Enter name");
	name = scan.nextLine();
	if (name == "jose") {
		System.out.println("Hello Jose!! =D");
		System.out.println("Why hello there stanger =3");

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


Thank you.

1 Like

You’re right and this is the expected output. This is because you’re using the wrong equality.

In Java, the == is testing for reference equality. Are they the same object?
An example of this would be

String name = scan.nextLine();
String newName = name
if (name == newName) {
  System.out.println("This will work!")

Now, there’s a super duper secret thing called .equals() which is testing for value equality. That’s the key ingredient for why your problem isn’t working.
Reading your current if statement it’s asking is the name object equal to the “jose” object. Which we know is not the case. If we said

if (name.equals("jose")) {
  . . .

We will get the output that you are expecting. This if statement is asking us: does name have the same value as “jose”.

Hopefully this will make more sense to you. I tried explaining it to the best of my ability.

1 Like

I believe @caleb-mabry is correct.

I was confused at first because that code definitely is not normal python :wink:

Most folks use indentation rather than curly braces. It is nonstandard.

1 Like

Uh, it’s not normal python, because it’s Java. I’ve changed the category to reflect that.