Comparing console input from executable jar [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
So I have this executable jar file with a username and password and runs but value doesn't ever continue correctly.
Here is an example:
public static void main(String[] args) {
System.out.println("Hello. I am a program created by Moocow9m. What is your name?");
InputStream stream = System.in;
Scanner scanner = new Scanner(stream);
String input = scanner.next();
if (input == "armystich"){
System.out.println("Welcome CODE NAME: ArmyStich!");
scanner.close();
}
else {
System.out.println("Hello " + input + ". Nice to meet you.");
scanner.close();
}
}
}
All would work except it would always return to else. Please help.

if (input == "armystich"){
The above checks reference equality. To check for value equality, using the equals method
if ( input.equals("armystich") ){
See How do I compare strings in Java?

Related

How do Java scanner read string correctly [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 1 year ago.
String command = "";
Scanner sc = new Scanner(System.in);
while (command!="exit")
{
System.out.println("Please enter command: ");
command = sc.nextLine();
System.out.println("You enter: "+command);
if (command == "exit")
{
System.out.println("Exit program.");
}
}
sc.close();
Haven't written java in a year and I forgot how scanner work. The code would never enter the if part when I enter exit. I tried next() and nextLine().
Because operator == compares the address memory not the value for String variable. You need to use equals() method.
command.equals("exit")

i keep getting the good bye response no matter if i input yes or no, how would i change that so responses are correct [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I keep getting the 'good bye' response no matter what I enter, 'yes' or 'no'. How would I change that so responses are correct.
import java.util.Scanner;
public class SimpleQuiz1 {
public static void main(String[] args){
Scanner userInputScanner = new Scanner (System.in);
System.out.println("Are you ready to take this NBA quiz?");
String answer = userInputScanner.nextLine();
if(
answer == ("yes")){
System.out.println("Then lets get started!!!");
} else{
answer = ("no");
System.out.println("Goodbye, come again soon!");
}
}
}
Use String.equals(Object obj) rather than == as "==" will check for reference equality whereas .equals compares only the content not the reference.
Scanner userInputScanner = new Scanner(System.in);
System.out.println("Are you ready to take this NBA quiz?");
String answer = userInputScanner.nextLine();
if (answer.equals("yes")) {
System.out.println("Then lets get started!!!");
} else {
this should work fine.

I want to break the loop by a string input . But the program is taking input infinitely [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I want to break the loop by a string input . But the program is taking input infinitely.
public static void main(String[] args) {
int i;
String test;
Scanner inputString = new Scanner(System.in);
while(1==1){
test=inputString.next();
if(test=="Break"){
break;}
}
System.out.println("the loop actually broke");
}
Try to use equals method instead of == like this:
if("Break".equals(test)){
You should use " test.equals("Break") " instead of " test=="Break" " because in Java " == " checks for reference equality(whether they are the same objects) but " equals() " checks for value equality(whether they are equal "logically")

Different output for same conditions [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Below is the code:
package com.myprograms.test;
import java.util.Scanner;
public class MyProgram {
public static void main(String[] args) {
Scanner sIn = new Scanner(System.in);
String name = "";
System.out.print("Enter the name:");
name = sIn.next();
if (name.equals("somename")) {
System.out.println("Success - Case 1");
} else {
System.out.println("Failed - Case 1");
}
if (name == "somename") {
System.out.println("Success - Case 2");
} else {
System.out.println("Failed - Case 2");
}
sIn.close();
}
}
Below is the output:
Enter the name: somename
Success - Case 1
Failed - Case 2
Here is the question:
Why one input behaves differently for the same condition in Java? Is it Java error?
"Java Error" - Really..??
This was explained many times, anyway here is a small explanation:
While comparing string in Java, use String.equals() or String.equlasIgnoreCase() methods.
Using == operator, compares the address.
While using equals() or equalsIgnoreCase() method compares the contents of the Strings.
Here is a good explanation (read "Meet Jorman" example):
An observation: You can close the Scanner after getting the inputs, instead of last line of the program, for better resource management.

While loop in Java, string comparison does not work [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
I'm working on a small program that asks for your name using a Scanner. If you enter blankstring, then I would like the console to display a message.
Here's what I tried doing:
import java.util.Scanner;
public class Adventure
{
public static void main(String[] args)
{
Scanner myScan = new Scanner (System.in);
System.out.println("What's your name?");
String name = myScan.nextLine();
while (!(name == "")) //Always returns false.
{
System.out.println("That's not your name. Please try again.");
name = myScan.nextLine();
}
System.out.println("It's a pleasure to meet you, " + name + ".");
}
}
The code never enters the while loop. Why?
Change your condition to:
while(!name.equals("")) {
or as suggested below by m0skit0:
while(!name.isEmpty()) {
See also
why equals() method when we have == operator?

Categories