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)
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed last year.
I want to know how to compare string to a variable.
For example I want to check if the input taken is run and if it's not print that is not a command
class Main
{
public static void main(String args[])
{
System.out.println("a pokrmon appeard");
System.out.println("what would you like to do");
Scanner scan = new Scanner(System.in);
Random genrator = new Random();
String input =scan.nextLine();
int genrate;
genrate=genrator.nextInt(4);
String fail ="failed to escape";
String escaped ="got away safly";
if (genrate <= 2){
System.out.println(escaped);
}
else{
System.out.println(fail);
}
};
I have tried using some methods like if(input=="run") but it doesn't work
Salam (Hello in Muslim) guy.
To compare a string with another string, you need to call the string1.equals(string2) method.
It compares the value of string1 with string2 and if the values match, it returns true otherwise false.
string1==string2 - compares references to objects of type string, not the values of these objects. You can read more about this here How do I compare strings in Java? . Good luck learning Java. (I'm from Russia, I'm writing from a translator)
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I am not sure why but when I get a string from the user, I cannot compare it in an if statement but when I try to print it, it works fine.
Part of my code:
Scanner in = new Scanner(System.in);
while (true) {
String userInput;
int rowInput, colInput;
printBoard(board);
System.out.print("Move: ");
userInput = in.next();
// shift board right on a row
if (userInput == "r") {
System.out.print("row #: \r");
rowInput = in.nextInt();
moveRight(--rowInput, board);
}
Does anyone know why this isn't working as expected?
You an try this:
if (userInput.equals("r"))
== is used to compare the address and equals is used to compare contents.
I should be using equals instead of ==.
So it would lead to:
...
if (userInput.equals("r"))
...
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
So, I'm not sure what is happening here, if I place encode or decode in the args[0], it should work right, but it doesn't. I have all the imports and I have a utility class that I'm using as well. I don't understand why, when I run the program with these arguments: java Prog4 encode fly message.txt it won't work right. It'll go straight to the last else statement.
public class Prog4 {
public static void main(String[] args){
if (args.length != 3){
System.out.println("Enter the right amount of arguments!");
System.exit(0);
}
String command=args[0];
String key= args[1];
String fileName = args[2];
File file = new File(args[2]);
String fileExtention="";
if(args[0]=="encode"){
fileExtention=".crypt";
}
else if (args[0]=="decode"){
fileExtention=".decrypt";
}
else{
System.out.println("Enter decode or encode!");
System.exit(0);
}
try this:
args[0].equals("encode")
and this:
args[0].equals("decode")
to compare the strings in java...
you use == to check if the references are equal.
you should use .equals() to check if the values are equal.
args[0]=="encode"
is WRONG!
it checks for object reference equality, not value equality!
use:
args[0].equals("encode");
or
args[0].equalsIgnoreCase("encode");
to ignore the case
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Noob Java question: Why won't this Do While loop accept the user input? When I use a different variation (such as int for the answer), it works. But when I look for a string, it never accepts the string and escapes the loop.
This works:
int value = 0;
do {
System.out.println("Enter a number: ");
value = scanner.nextInt();
}
while(value != 5);
System.out.println("Do while loop has ended.");
This doesn't work:
String pass;
String word = "word";
do {
System.out.println("Enter password: ");
pass = scanner.nextLine();
}
while(pass != word);
System.out.println("Password accepted.");
Thanks
Change this:
while(pass != word);
to this:
while(!pass.equals(word));
You were comparing the references when you used !=, not the actual content of the strings. Since they did not point to the same String, your loop would always exit on the first run.
"==" compares addresses in memory so if you enter the word which will be the same, the reference you have stored will point to different object.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have a portion of code that should do that:
1) Take the name of a color from keyboard
2) Give that color to an object in wich i have another object declaration
3) Decrement from 2 to 1 the correspondant line of the array of 2.
In the main class i want to memorize the color in a variable TemporaryColor.
Scanner input = new Scanner(System.in);
String TemporaryColor = input.nextLine();
playerOrd[1].DecrementoSegnalino(TemporaryColor);
playerOrd is a class that contains this method:
public void DecrementoSegnalino(String color) {
SegnalinoScommessaGiocatore.decrementaSegnaliniScommessa(color);
}
SegnalinoScommessaGiocatore have this array:
private int[] numeroSegnaliniScommessa = {2,2,2,2,2,2};
and this method:
public void decrementaSegnaliniScommessa(String color) {
if (color.equalsIgnoreCase("Black") numeroSegnaliniScommessa[0]--;
if (color.equalsIgnoreCase("Blue") numeroSegnaliniScommessa[1]--;
if (color.equalsIgnoreCase("Green") numeroSegnaliniScommessa[2]--;
if (color.qualsIgnoreCase("Red") numeroSegnaliniScommessa[3]--;
if (color.equalsIgnoreCase("Yellow") numeroSegnaliniScommessa[4]--;
if (color.equalsIgnoreCase("White") ) numeroSegnaliniScommessa[5]--;
}
There is a problem passing the string that i write with Keyboard...
If i use this in the beginning:
playerOrd[1].DecrementoSegnalino("Black");
It works!
What's the problem?
Don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of
if (fu == "bar") {
// do something
}
do,
if ("bar".equals(fu)) {
// do something
}
or,
if ("bar".equalsIgnoreCase(fu)) {
// do something
}