This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
i want to ask a question for a bedsize and while the answer is not what i choose it will be i want that it will ask the user to answer again
import java.util.Scanner;
public static void main(String[] args) {
String newBedType ;
Scanner sc1 = new Scanner(System.in) ;
System.out.println("you want a single bed or doublebed? ") ;
newBedType = sc1.next() ;
while (newBedType != "single" + "doublebed") {
System.out.println("please choose againe the bed size: ");
newBedType = sc1.next() ;
switch (newBedType) {
case "single" : System.out.println("i see you like sleeping alone");
break ;
case "doublebed" : System.out.println("got company ;) ");
break ;
}
}
}
}
the code kinda works it shows the cases if i write the correct string but it will continue to ask me forever.....
i just stared learning java so be easy on me i know its a stupid question but after hours of trying and searching here(though i did found in python but dont know how to "translate" it to java)
i cant figure it out... thanks to anyone willing to help :)
Your issue is with the line:
while (newBedType != "single" + "doublebed")
This doesn't do what you think it does. You are comparing the variable newBedType with the string "singledoublebed", the addition operator is concatenating those two strings. You want the line:
while (!newBedType.equals("single") && !newBedType.equals("doublebed"))
Note the use of the .equals() method, as string comparisons in Java do not act as expected with the == or != operators.
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 am trying to create the following program in which I need to turn the input String's characters into lowercase to ease my work.
I tried using toLowerCase(); first but it didn't work. Then I tried using toLowerCase(locale); and yet I have not succeeded.
public static void Mensuration() {
Locale locale = Locale.ENGLISH;
Scanner inputz = new Scanner(System.in);
System.out.println("Which kind of shape's formula would you like to find out.2D or 3D?");
char dimension = inputz.nextLine().charAt(0);
if(dimension == '2') {System.out.println("Okay so which shape?");
String dimensiond = inputz.nextLine().toLowerCase(locale);
if(dimensiond == "rectangle") {System.out.println("Area = length x breadth\nPerimeter = 2(length + breadth)");}
}
}
I expected the program to give the accurate output but the thing that happens is that there is no output actually!!
use equals to compare strings. I think this causing the error
"rectangle".equals(dimensiond)
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I want to ask a question.
I feel so confused about my own coding because i think it is correct.
This is the issue.
public static void main(String[] args) {
String x = "Robert : Hi There";
String y = "Robert";
System.out.println(x.substring(0, x.indexOf(":")).trim());
if(x.substring(0, x.indexOf(":")).trim() != y){
System.out.println("Pass");
}
else
{
System.out.println("Not Pass");
}
}
This gave me output:
Robert
Pass
I want the output is "Not Pass" but why did my coding gave another result.
I hope you can tell what is wrong.
Thank You.
You compare string objects. So you have to ue the equals method:
if(x.substring(0, x.indexOf(":")).trim().equls(y)){
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I wrote a simple program which prompts the user to enter a sentence, word, or number, and then prints input is art. I put the program on an infinite loop so that the prompt would repeat. However, now I'm trying to add a way to quit by saying if (input == "quit") break; however it does not seem to be working. It just continues the infinite loop. Here is the full code:
import java.util.Scanner;
public class Art
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
String a;
for ( ; ; )
{
System.out.println("Please enter something: ");
a = input.nextLine();
if (a == "quit") break;
System.out.print("\n");
System.out.println(a + " is art.");
System.out.print("\n");
}
}
}
Any help would be lovely, thanks!
Use input.equals("quit").
The == is used to check whether two objects are the exact same instance - the same thing in memory -which the typed word and the constant string "quit" are not. Two instances of "quit" were created: one typed by the user, and one constant in the program.
The equals() method is used to compare whether two objects are equal in whatever way equality is defined for them. For strings, that would mean having the same text.
The difference between == and equals() is really fundamental in Java, so you should go over it. Here's a good SO post on the topic. Once you start creating your own classes, you will probably be implementing equals() methods for them. For that reason, make sure to go over the equals() / hashCode() contract as well.
looks like the a == "quit" is the problem. Compare strings using equals()
if("quit".equals(a)) {
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
String.equals versus == [duplicate]
(20 answers)
Closed 9 years ago.
So I'm fairly new to Java and I am trying to run a program that will display a certain number of letters from a name and ask the user for a response. The user's response should determine one of two answers ("Correct" or "I'm sorry, that's incorrect").
The problem I'm encountering is that when I run the program and put in the answer that should lead to "Correct," which is 'Billy Joel,' I get the response of "I'm sorry, that's incorrect."
I'm not actually sure what's going on, but here's a link to a picture of the CMD when I input what should lead the system to say "Correct" and instead it says "I'm sorry, that's incorrect":
And here is a copy of the relevant code:
System.out.println("\nLet's play Guess the Celebrity Name.");
String s6 = "Billy Joel";
System.out.println("\n" + s6.substring(2, 7));
Scanner kbReader3 = new Scanner(System.in);
System.out
.print("\nPlease enter a guess for the name of the above celebrity: ");
String response = kbReader3.nextLine();
System.out.println("\nYou entered: \n" + response + "\n");
if ((response == "Billy Joel")) {
// Execute the code here if Billy Joel is entered
System.out.println("\nCorrect!");
} else {
// Execute the code here if Billy Joel is not entered
System.out.println("\nI'm sorry, that's incorrect. The right answer was Billy Joel.");
}
System.out.println("\nThank you for playing!");
There's more before this that the program also does, but I'm not having a problem with any of that and it's all correct. I took out the Billy Joel part and everything else ran exactly as it was supposed to. It's just the above code in relation to what it should put out and what it is putting out that's the problem. I'm wondering if maybe I'm missing something in my code or I put something in wrong, but whatever I did, help would be much appreciated.
Your problem lies here. You are using wrong operator to compare strings
if ((response **==** "Billy Joel")) {
System.out.println("\nCorrect!");
} else { ... }
the correct should be
if ((response.equals("Billy Joel")) {
System.out.println("\nCorrect!");
} else { ... }
To compare strings in java you have to use .equals() operator. And to use '==' operator you need to have int, bool etc.
if (response!=null && response.length>0){
//trim the input to make sure there are any spaces
String trimmed=response.trim();
if (response.equals(s6))
System.out.println("\nCorrect!");
} else { ... }