This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
i got a little problem with my code. When i write "exit" still shows statement "Unknown command" and i want to show "Bye". Can you help me?
import java.util.Scanner;
public class Hello{
public static void main(String[] args){
Scanner odczyt = new Scanner(System.in);
String word;
do{
word = odczyt.nextLine();
System.out.println("Unknown command");
}
while(word!="exit");
System.out.println("Bye");
}
}
"Unknown command" will always be printed. Besides that you shouldn't use = to compare Strings. You should use .equals() or .equalsIgnoreCase().
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
This is what I have so far and I tried to use the while (yorn=="yes") to cancel but it keeps going regardless and I am also trying to find a way to make it so the outputs go in one group together rather than being seperated.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String no = "no";
String yes = "yes";
String yorn="yes";
CollegeStudent Student1 = new CollegeStudent();
while (yorn=="yes") {
System.out.println("continue? yes/no:");
yorn=scan.next();
Student1.setname();
Student1.setcourseTitle();
Student1.setcredits();
Student1.setcourseCode();
System.out.println(Student1.toString());
}
}
}
You need to use the equals method for string comparing in the java:
while (yorn.equals("yes"))
{
System.out.println("continue? yes/no:");
yorn=scan.next();
Student1.setname();
Student1.setcourseTitle();
Student1.setcredits();
Student1.setcourseCode();
System.out.println(Student1.toString());
}
Since after you answer exists the logic in the cicle, after yes typing code has been executing anyway (one time). Simply solution for you will be:
System.out.println("Do you need to add a student? yes/no:");
yorn=scan.next();
while (yorn.equals("yes"))
{
Student1.setname();
Student1.setcourseTitle();
Student1.setcredits();
Student1.setcourseCode();
System.out.println(Student1.toString());
System.out.println("continue? yes/no:");
yorn=scan.next();
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
I'm trying to make a program that can read whatever the user inputs and checks their input using if..else statement.
import java.util.Scanner;
class Answers{
public void FalseAnim() {
System.out.println("Game Over your answer is wrong. try again!");
}
public void CorrectAnim() {
System.out.println("your answer is correct");
}
}
public class quizgame {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
Answers ans = new Answers();
String strans1;
System.out.println("welcome to the quiz game!");
System.out.println("what is 1+1");
strans1 = input.nextLine();
if (strans1=="two"||strans1=="2") {
ans.CorrectAnim();
}
else {
ans.FalseAnim();
}
}
}
every time I run the program and input anything it goes straight into the else statement, even when I input either a "2" or a "two"
if ("two".equals(strans1)||"2".equals(strans1))
will work.
In your code, you are comparing references and not the value. Hence it is returning false either way.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
public static void main(String args[]){
System.out.print("Type new, wil display TRUE:");
Scanner sc = new Scanner(System.in);
String userInput = sc.nextLine();
if(userInput=="new"){
System.out.println("TRUE");
}else {
System.out.println("FALSE");
}
}
I have no idea why new not equals to new.
Please give me some hints :)
Use userInput.equals("new") instead.
This explains everything: How do I compare strings in Java?
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
import java.util.Scanner;
public class RandomTest {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String[] alphabets={"a","b"};
String [] cipher={"b","c"};
System.out.println("Please enter a letter.");
String word=input.nextLine();
for (int i=0;i<2;i++){
if (word.equals(alphabets[i])){
System.out.println("Preparing a cipher");
System.out.println("Here is the cipher: "+cipher[i]);
}
}
}
}
This code above works perfectly fine, but instead of saying (word.equals(alphabets[i])), if i put word==alphabets[i] it wouldn't work at all. While using the later, the program does not check if the input is equal to a value in the array. Why does this happen?
== is an operator which compares the objects' locations in memory. equals() is a method defined in Object meant for comparing the actual content/values. By default, these two options behave similarly, but Java's String class overrides equals() when comparing strings.
In the future, please do research before asking questions. There is plenty of information to be found on the Internet.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I want user to input text while it is not equal to "start".When it is equal to "start" I want to show "Bravo".In my code when I enter "start" it just continue to ask to input a text.What is missing in my code to process the operation i described.
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String komanda = "a";
do {
System.out.println("Unesi komandu ");
komanda = input.nextLine();
}
while(komanda != "start");
System.out.println("Bravo");
}
}
You have to use the equals method to compare strings in java:
while (!komanda.equals("start"));
or even better
while (!"start".equals(komanda));
this does not crash if komanda is null
See How do I compare strings in Java? for more information.
do it this way
Scanner input = new Scanner(System.in);
String komanda = "a";
do {
System.out.println("Unesi komandu ");
komanda = input.nextLine();
}
while(!"start".equals(komanda));
System.out.println("Bravo");