Strings from input are never equal? [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
import java.util.Scanner;
public class Michal {
public static void main(String[] args) {
reply();
}
public static void reply() {
Scanner input=new Scanner(System.in);
String name=input.nextLine();
if(name=="john"){
System.out.println("bear!");
}else if(name=="mary")
{
System.out.println("lovely lady!");
}else{
System.out.println("I don't know that person.");
}
System.out.println(name);
input.close();
}
}
I consider myself still a beginner to Java ,so please don't be harsh in your answers. I was trying to create a program that returns an answer every time it gets a certain input , however it seems to return "I don't know that person" all the time.

if(name=="john")
is not the right way to compare strings. Use equals() instead:
if(name.equals("john")){
System.out.println("bear!");
} else if(name.equals("mary")){

Related

Printing 2 different variables in the same line Java VM [duplicate]

This question already has an answer here:
How to print both bool and double in same line? [duplicate]
(1 answer)
Closed 2 years ago.
I am new to java and i have 2 different Variables one is a double and one is a boolean. I am having difficulty printing them into the same line, this is my code.
class Main {
public static void main(String[] args) {
boolean isTrue;
isTrue = false;
double money;
money = 99999.99;
System.out.println(isTrue, double);
}
}
Thanks a lot if you answer
Many ways to do this and if you visited the javadocs you would see that there is also System.out.print which does not do a line feed, so you could use that as
System.out.print(isTrue);
System.out.println(money);
Or you could use System.out.printf, or convert values into a String first
There are few methods to do this, one is mentioned in the above answer and the other is below, that you can do this with a professional approach by using printf.
public static void main(String[] args) {
boolean isTrue;
isTrue = false;
double money;
money = 99999.99;
System.out.printf("%b%.2f%n", isTrue, money);
}

Java if statements with user input (beginner) [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
This is just something im trying out, i just recently started coding in java would like some help. Would like to ask user to pick their favorite movie, then take their input and use if statements to give different responses to each response.
import java.util.Scanner;
public class miniFFF {
public static void main (String[]p) {
System.out.println("What is your favourite movie? pick from the answers below:");
System.out.println("a");
System.out.println("b");
System.out.println("c");
System.out.println("d");
System.out.println("e");
answer();
//ifStatements();
System.exit(0);
}
public static String answer() {
String favMovie;
Scanner test = new Scanner(System.in);
favMovie = test.next();
if(favMovie == "a") {
System.out.println("1234");
}
else if (favMovie == "b") {
System.out.println("123");
}
return favMovie;
}
}
Using switch will make your code much readable.
switch (favMovie){
case "a": //do for a
break;
case "b" //do for b
break;
default: //no match
}
Java enum was designed for when you have a finite set of options. As good practice it will be a better option over string literal
I don't really see a question there and your code seems, I could point two thing I might do differently tho.
One I would use equalIgnoreCase instead of == in case someone doesn't put the correct capitalization (Also add a general case)
Also putting the method outside main seems to be better practice.

how to check if the variable is null? [duplicate]

This question already has answers here:
Check whether a String is not Null and not Empty
(35 answers)
Closed 5 years ago.
I know this doesnt work but how can I know if the user added even one char?
public class Program
import java.util.Scanner;
{
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String b = a.nextLine();
//I know this doesnt work but how can I know if the user added even one char?
if (b!=null){
System.out.println(b);
}
}
}
you can use .equals("") or .isEmpty()
check check if the variable is null
You can do this
if (!b.isEmpty()) {
System.out.println(b);
}

Why does this Java code behave as it does? [duplicate]

This question already has answers here:
Static Block in Java [duplicate]
(7 answers)
Closed 5 years ago.
Why does the following code print "Bo-Bo Go-Go", instead of "Bo-Bo Hello, World! Go-Go?
public class Test {
static {
System.out.print("Bo-Bo ");
}
public static void main(String[] args) {
System.out.print("Hello, World! ");
}
static {
System.out.println("Go-Go ");
System.exit(0);
}
}
Because static initialization blocks run before the entry-point (both of them), and the second one exits thus main is never entered.

Reading keystrokes from a keyboard and using if statement in Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I am learning how to program in Java. I wrote the following code but, I can't get the if statements to work when the variable inputName is compared to a string and the statement is true. Does anyone have any tips on how to fix this?
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
String x = ("Hello type in your first name.");
System.out.println(x);
Scanner keyboard = new Scanner(System.in);
String inputName = keyboard.next();
if (inputName == "erick") {
System.out.println("Hi");
}
String v = "victor";
if (inputName == v) {
System.out.println("Hi");
}
String c = "christy";
if (inputName == c) {
System.out.println("Hi");
}
keyboard.close();
}
}
don't use == instead use equals() for string comparison.
== will check whether 2 references are referring the same object, while equals() will check the value being referred to.

Categories