This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
When I create a variable and wrap the code in a while loop it never repeats. Here's a sample of code I tried it on.
String repeat = "y";
Scanner keyboard = new Scanner(System.in);
while (repeat == "y"){
String word1 = "this";
String word2 = "that";
String word3 = word1 + word2;
System.out.println(word3);
for(int x = 10; x<20; x = x+1){
word3 = word1 + word3;
System.out.println(word3);
}
repeat = keyboard.nextLine();
}
No matter what the input is in the end of the script, it just ends. Any help?
Change the line
while (repeat == "y")
to
while("y".equalsIngnoreCase(repeat))
and
keyboard.nextLine() ;
to
keyboard.next();
Reading How do I compare strings in Java? will be helpful.
When you compare a string using '=='. you are comparing the object reference. You are essentially asking if the two objects are the same, rather than comparing the string contents. Try using the String.compareTo(..) method.
Example:
while (repeat.compareTo("y") == 0) {
The problem probably comes from the facts you compare two objects with = but I guess what you want to do is compare Strings with the String method equals, so it would look like that:
while(repeat.equals("y")){
...
}
Never use == to compare strings.
Try this:-
while ("y".equalsIngnoreCase(repeat) )
Related
This question already has answers here:
How does compareTo work?
(3 answers)
How to correctly compute the length of a String in Java?
(5 answers)
Closed 3 months ago.
This code is to compare 2 strings entered by the user and it's supposed to output whether the first string is > < or = the second string.
It's working for the most part except when I enter two phrases like gh and hi, it thinks gh is greater than hi. Maybe it's looking at the size of the actual letter.
package com.mycompany._3;
import java.util.Scanner;
public class App {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first phrase(s): ");
String one = scanner.next();
System.out.print("Enter Second phrase(s): ");
String two = scanner.next();
int length = one.compareTo(two);
if(length > 0){
System.out.print("String one is less than string two.");
}else if (length < 0)
System.out.print("String one is greater than string two.");
else
System.out.print("Both phrases are equal length");
}
}
The compareTo() method compares two strings lexicographically.
The comparison is based on the Unicode value of each character in the strings.
You need to call length() method for each string to compare lengths
if(one.length() > two.length()) ...
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
I wanted to find out in options.bldata if firstLaunch= was written, for this I wrote the following code:
File file = new File("options.bldata");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line == "firstLaunch=") {
System.out.println("123");
}
}
when it finds a line with firstLaunch= it should print 123, but I don't know why it doesn't print 123 even if firstLaunch= is in the file.
Operator == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equals("firstLaunch=")) {
System.out.println("123");
}
}
Here is an article for more information
There is two wrong thing in the code.
Never use double equals to compare strings. You need to use the equals() function from string to do so
you want to test if the string firstLaunch= is in the line, not that your line is equals to firstLaunch=. For this, you can use line.contains("firstLaunch=")
You should use equals() to compare strings. Or in your case, contains() because the line might have other stuff, not just what you want to find
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
import java.util.Scanner;
public class main {
public static void main(String[] args) {
boolean a = true;
do {
Scanner input = new Scanner(System.in);
System.out.println("Press any on keyboard:");
String keys = input.nextLine();
System.out.println("You pressed:");
System.out.println(keys);
System.out.println("Your hash is:");
String B = "#B";
String hash = B+keys;
System.out.println(hash);
System.out.println("To end loop press f");
//End Loop
Scanner exit = new Scanner(System.in);
String end = exit.nextLine();
if (end=="f") {
a=false;
}
}
while(a);
}
}
I've been using python and I decided to start learning java since android studio requires it. I'm learning how to do loops again. I can't get this to work. I already looked this up I couldn't find it. How would I end this by pressing 'f'? My thought process was that once it was done going though the first lines of the do loop, it would go though the if statement changing the value of a ending the loop.
use break statement under if(){} body. also your == comparison will give false, use str1.equals(str2) for comparison.
Your problem is you are comparing strings with ==.You have to use equals to write correct if statement.
if (end.equals("f")){...}
You could use the below code to check
if (end.equals("f")) { // end == "f" , it check the reference.
a = false;
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I've got a problem with looping a scanner, wich should return EVERY string given as an input unless String != "end". Here is what I've done so far
private static String fetchString() {
Scanner scanner = new Scanner(System.in);
String stringElement = "";
System.out.println("Write a string");
while(scanner.hasNextLine()) {
stringElement = scanner.next();
if(stringElement == "end") {
break;
}
}
return stringElement;
}
result:
Write a string
abc
abc
abc
end
end
Loop, somehow, doesn't understand if(stringElement == "end"), it still wants new word. I can't get it. Where am I making a mistake?
First change, stringElement = scanner.next(); to stringElement = scanner.nextLine();, second change if(stringElement == "end") to if(stringElement.equals("end"))
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I'm trying to create a program which takes user input of two words and determines whether or not these words are the same.
import java.util.Scanner;
public class L7E3 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System. in );
String word1, word2;
System.out.println("Please enter a word: ");
word1 = keyboard.nextLine();
System.out.println("Please enter a word: ");
word2 = keyboard.nextLine();
if (word1 == word2) {
System.out.println("The words are " + word1 + " and " + word2 + ". These words are the same.");
} else {
System.out.println("The words are " + word1 + " and " + word2 + ". These words are not the same.");
}
}
}
I figured that word1==word2 would have worked to determine whether the two strings were equal, I'm using JGrasp and it goes directly to my else option regardless of input. Am I doing something wrong with strings?
if(word1.equals(word2))
== doesn't do what you think it does. == essentially compares the memory locations of the two String variables and returns true only if they're located at the same memory location. The String.equals method compares the contents of the strings and returns true if they hold the same characters.
Short answer: use String#equals(Object) instead:
word1.equals(word2)
For a super detailed explanation to the reasoning why: check out this.
For Strings you need to use the .equals() function rather than the == equality operator.
if(word1.equals(word2))
If you wanted to test if two words are the same, while ignoring case ("This" is the same as "this) then you need to do something like this:
if (word1.toLowerCase().equals(word2.toLowerCase()))
Also in your specific example you might want to remove unnecesary whitespace from before and after the word (" word1 " should becomes "word1"). You can do this using the trim() function:
word1 = word.trim();