This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
I am trying to solve https://www.codechef.com/problems/FLOW010 problem.
I wrote
`
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t>0){
String s=sc.next();
if(s=="b" || s=="B"){
System.out.println("BattleShip");
}
else if(s=="c" || s=="C"){
System.out.println("Cruiser");
}
else if(s=="d" || s=="D"){
System.out.println("Destroyer");
}
else if(s=="f" || s=="F"){
System.out.println("Frigate");
}
t--;
}
}
`
There is no mistake in terms of syntax. Please help me what is mistake
Replace all your == comparisons for String like this:
String s=sc.next();
if(s.equalsIgnoreCase("b") {
System.out.println("BattleShip");
}
// ... etc.
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 1 year ago.
import java.util.Scanner;
public class CheckPalindrome3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your number :");
String num = input.nextLine();
String arr = "";
for(int i = num.length() - 1 ; i >= 0;i--)
{
arr = arr + num.charAt(i);
}
System.out.println(arr+"\n"+num);
if(arr == num) {
System.out.println("Yes that's a Palindrome !!");
}
else
{
System.out.println("No that's not a Palindrome !!");
}
}
}
When I put 121 it printed out No thats not a palindrome but arr is the same as num
The problem is the test arr == num. For strings, == tests whether the two strings have the same address in memory (which often is not true, even if the strings look the same). Use arr.equals(num) instead. That will test if all of the characters in arr are the same as those in num.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
The line starting the if statement is causing me issues. When I run it it will ask me my gender, I say boy and it says 'you cannot enter'. It doesnt even ask for the age!
import java.util.Scanner;
class Apples {
//////Main Method
public static void main (String[] args) {
if (Apples.getGender() == "boy" && Apples.getAge() > 17) {
System.out.printf("You can enter");
}
else {
System.out.printf("You cannot enter");
}
}
/////Two Methods
public static String getGender() {
Scanner newGender = new Scanner(System.in);
System.out.println("What gender are you?");
String genderMF = newGender.nextLine();
return genderMF;
}
public static int getAge() {
Scanner newAge = new Scanner(System.in);
System.out.println("What age are you?");
int ageMF = newAge.nextInt();
return ageMF;
}
}
You have to compare strings with .equals function in Java, not with ==.
s1.equals(s2);
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Could some one please tell me why this calculator isn't working ? It just doesn't provide an awnser.
import java.util.Scanner;
public class Calcu {
public static void main( String[] args )
{
Scanner mati = new Scanner(System.in);
System.out.println("This program adds up or substracts two numbers");
System.out.println("Enter an operator");
String letter = mati.next(); //WAITS FOR THE PHRASE ADD OR SUBSTRACT
System.out.println("Enter your first number");
int userNumberone = mati.nextInt(); // Get's first Number
System.out.println("Enter your second number");
int userNumbertwo = mati.nextInt(); //Get's Following Number
if(letter == "add") {
int result = userNumberone + userNumbertwo;
System.out.println(result);
} else if(letter == "substract") {
int result1 = userNumberone - userNumbertwo; //If statement to add or substract.
System.out.println(result1);
}
}
}
You should use letter.equals("add") instead of letters == "add". This is explained here: How do I compare strings in Java?
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I am making a Game of Life program, early stages of it. When I run the program and get to the "Do you want to make..." and i input "y", it will go to the else, print my test statement of test3, and end the program. What am I overlooking?
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String userInput = "";
char[][] initialGrid = new char[25][75];
char[][] world = makeInitialGrid(kb, userInput, initialGrid);
printGrid(world);
userInput = "y";
while (userInput == "y"){
System.out.println("Do you want to make a new generation? (y) yes (n) no");
userInput = kb.nextLine();
System.out.println(userInput);
if (userInput == "y"){
System.out.println("test1");
int numOfNeighbors = findNeighbors(world, 6, 2);
System.out.println("test2");
System.out.println(numOfNeighbors);
//makeNewGeneration(world);
} else {
System.out.println("test3");
break;
}
}
kb.close();
For string comparisons in Java, you need to use String#equals, not ==. Try if (userInput.equals("y")) { ... instead.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Equal strings aren't equal (==) in Java? [duplicate]
(3 answers)
Closed 9 years ago.
import java.util.Scanner;
public class NewClass {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name");
String name = scanner.nextLine();
System.out.println("Is "+ name + " really your name?");
String answer1 = scanner.nextLine();
if (answer1 == "yes"){
System.out.println("Alright ");
}else {System.out.println("Liar!");
}
}
}
It outputs Liar! even though I typed yes, so that answer1 equals yes. Why?
Because with == you are testing reference equality, not value equality. Good reading.
Instead answer1 == "yes" do answer1.equals("yes").