This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String play = "y";
System.out.print("Enter something: ");
play = scan.next();
System.out.println(play);
if (play == "Y" || play == "y")
{
System.out.println("If test works!!");
}
System.out.println("Did it work???");
}
}
I assume this has something to do with when I press enter, it's storing that as well. I tried changing String play to a char, but then I get errors from Scanner saying it can't change a String to a char.
You should atmost avoid using “==“ when comparing objects especially strings. “==“ checks for object references. Change the comparison to use .equals method and it should work
if(play.equals(“Y”) || play.equals(“y”))
in case if “play” can be null, the below snippet is more safe.
if(“Y”.equals(play) || y.equals(play))
Related
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 can I avoid ArrayIndexOutOfBoundsException or IndexOutOfBoundsException? [duplicate]
(2 answers)
Closed 7 years ago.
Whenever I try to use anything higher than eq[0], I end up with an ArrayIndexOutOfBoundsException.
My code:
import java.util.Scanner;
public class Calc{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String[] eq=in.next().split(" ");
double a=Double.parseDouble(eq[0]);
double b=Double.parseDouble(eq[-1]);
if(eq[1]=="+"){
System.out.println(">>"+String.valueOf(a+b));
}else if(eq[1]=="-"){
System.out.println(">>"+String.valueOf(a-b));
}else if(eq[1]=="/"){
System.out.println(">>"+String.valueOf(a/b));
}else if(eq[1]=="*"){
System.out.println(">>"+String.valueOf(a*b));
}
}
}
eq[-1] -1 index is problem
eq[1]=="+" // logical error use equals() method to compare String.
Scanner in=new Scanner(System.in);
String[] eq=in.nextLine().split(" "); // use nextLine() instead of next().
double a=Double.parseDouble(eq[0]);// 1st operand
double b=Double.parseDouble(eq[2]);// 3rd operand
if(eq[1].equals("+")){ //operator // compare string with equals method not with (==).
System.out.println(">>"+String.valueOf(a+b));
}else if(eq[1].equals("-")){
System.out.println(">>"+String.valueOf(a-b));
}else if(eq[1].equals("/")){
System.out.println(">>"+String.valueOf(a/b));
}else if(eq[1].equals("*")){
System.out.println(">>"+String.valueOf(a*b));
}
Firstly, eq[-1] the index should not be valid. I think it is only valid for Python.
Secondly, when you want to compare 2 strings, you should not use ==.
.equals function will help.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I'm writing a simple code to test the value that was inputted to my constant value.
I declared this code as my constant value.
String LetMeThrough = "drunk";
String GotAnID = "drunk";
This is the whole code.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner DrunkTest = new Scanner(System.in);
String InputDrunk;
String InputDrunkAgain;
String LetMeThrough = "drunk";
String GotAnID = "drunk";
System.out.print("Type drunk: ");
InputDrunk= DrunkTest.next();
System.out.print("Re Type drunk: ");
InputDrunkAgain = DrunkTest.next();
if(InputDrunk == LetMeThrough & InputDrunkAgain == GotAnID){
System.out.print("You're not DRUNK");
}
else
System.out.print("You're F***** DRUNK");
}}
The problem is that if I type "drunk" on both.
I will get "You're F****** DRUNK" instead of the "You're not DRUNK".
When the inputted values is the same as my constant values.
You must use String::equals method to compare.
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");
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.
package temperatureconversion;
import java.util.Scanner;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter Conversion type: Press C for Celsius to Fahrenheit or press F For Fahrenheit to Celsius.");
String Vctype = keyboard.next();
if (Vctype == "f" || Vctype == "F"){
System.out.println("Please enter fahrenheit");
double Vfahrenheit = keyboard.nextInt();
Vfahrenheit = (Vfahrenheit)*(9/5)+(32);
System.out.println(Vfahrenheit);
}
if (Vctype == "c" || Vctype == "C"){
System.out.println("Please enter celcius");
double Vcelcius = keyboard.nextInt();
Vcelcius = (Vcelcius - 32)*(5/9);
System.out.println(Vcelcius) ;
}
}
}
Hello guys I was wondering if anyone could help me with the above code. Basically in the output console in netbeans the program just seems to end after I hit C or F, but instead it should ask for a number then allow a number input, then calculate and finally display the calculation. It doesn't seem to be executing the if statements Where am I going wrong?
You are comparing String with ==. So it doesnt work, you have to use this :
if (Vctype.toLowerCase().equals("f"))
Note also, that using a "toLowerCase" makes the whole string lowercase, so you dont have to have two options for "F" and "f".
If you want, you can use "compareTo"
if (Vctype.toLowerCase().compareTo("f") == 0)