This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
This is the first section of my main method, where I prompt the users to input as many phrases as they want, and the input gets saved in an ArrayList. I coded this so that the program would stop asking for more input if the user puts 'q', but for some reason, when I run this, even if I input q, it doesn't exit the loop. It just keeps on asking for more inputs. I'm assuming I made a mistake in the while loop, but I'm not entirely sure what.
I'm really new to Java so I may have made a blatant mistake but I'm not sure..
public static void main(String [] args)
{
//vars
String userInput;
int quitter = 0;
//arraylist
ArrayList<RecursivePalindrome> a = new ArrayList<RecursivePalindrome>();
//scanner
Scanner in = new Scanner(System.in);
//user input
System.out.println("\t Palindrome Checker");
System.out.print("Input phrase one at a time (press 'q' to quit): ");
while(quitter == 0)
{
userInput = in.next();
if(userInput == "q" )
{
quitter++;
}
a.add(new RecursivePalindrome( userInput));
System.out.print("Input phrase one at a time (press 'q' to quit): ");
}
System.out.println();
Comparison of strings
use .equals
if(userInput.equals("q"))
{
quitter++;
}
Compare the String with equals() and put break it will exit the while loop
if(userInput.equals("q" ))
{
quitter++;
break;
}
Related
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))
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I am creating a program that simulates Rock, Paper, Scissors, and I need the user to input either "Rock","Paper", or "Scissors". My Code is:
Scanner input = new Scanner(System.in);
String userIn = null;
while ((userIn != "Rock") || (userIn != "Paper") || (userIn != "Scissors")) {
System.out.println("Please Type either: Rock, Paper or Scissors");
userIn = input.next();
}
I created a scanner for the input and set the initial input to null. When the program runs, since the input is not either "Rock","Paper", or "Scissors", it will prompt the user to enter one of those three, the problem is even when I enter "Rock","Paper", or "Scissors" correctly, it still reprompts me to "Please Type either: Rock, Paper or Scissors".
what am I doing wrong?
For string comparisons use
userIn.equals("yourString") function rather than != comparisons.
You should try using .equals() or better .equalsIgnoreCase() if you are not bothered about the case of the input string. That might help.
equal or equalsIgnoreCase uses the content of object to make compare,but != do the comparison with the reference address
so the code should be
while ((userIn.equalsIgnoreCase("Rocker")) || (userIn.equalsIgnoreCase("Paper") || (userIn.equalsIgnoreCase("Scissors"))
Update your program like this,
Scanner input = new Scanner(System.in);
String userIn = "";
while (!(userIn.equals("Rock") || userIn.equals("Paper")|| userIn.equals("Scissors"))) {
System.out.println("Please Type either: Rock, Paper or Scissors");
userIn = input.next();
}
firstly, if you are doing
String userIn = null; //instead String userIn = " ";
we can't check
userIn.equals("Rock"); // i.e. null.equals("Rock");
compiler will through a null pointer exception. So, edit your program as above and will work properly.
This question already has answers here:
Trying to restart a do-while loop with a String variable to equal "yes" or "no"
(3 answers)
Closed 8 years ago.
I already have the code for the scanner but I don't know how to make the code so that you can ask the user whether to run the program again or not. My program is for the PigLatin (a constructed language game in which words in English are altered according to a simple set of rules).
I have to make it so that it will ask the user if he/she wants to translate another phrase and wait for the user to enter "Y" or "y" as yes, "n" or "N" as no. If yes, run program again. If no, exit the program. For all other letters, reject it and ask the user to enter only "y" or "n".
public static void main (String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter an English phrase or sentence :");
String sentence = keyboard.nextLine();
System.out.println("\"" +sentence+ "\"" + " ");
Scanner word = new Scanner(sentence);
System.out.println("In PigLatin that would be: ");
while (word.hasNext()) {
String pigLatin = word.next();
System.out.print(convertPigLatinWord(pigLatin));
}
}
Just use a while loop. Until you say "no" at the end, stop will be false, so !stop will be true, and you'll keep looping.
Scanner scan = new Scanner(System.in);
boolean stop = false;
while(!stop) {
//do whatever
System.out.println("Would you like to continue? (yes or no)");
String s = scan.nextLine();
if(s.equals("no")) {
stop = true;
}
}
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)
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
import java.util.Scanner;
public class OrigClass {
public static void main (String[] args){
Scanner ScanObj = new Scanner(System.in);
System.out.println("Set Password: ");
String SetPass = ScanObj.nextLine();
System.out.println("Guess Password: ");
String GuessPass = ScanObj.nextLine();
while (GuessPass != SetPass){
System.out.println("Incorrect. Try Again: ");
GuessPass = ScanObj.nextLine(); }}}
So as you can see I'm trying to make a very simple program that lets the user enter a password then re-enter it. I'm a little confused as to why this isn't working. I've tried everything I can think off and have read up on other similar problems on here, but can't find anything...
Sorry if this is a bit basic, but it's annoying me that I can't get this to work :(
You should compare the string with String.equals
so your code should look like this :
while (!GuessPass.equals(SetPass)){
and as mentionned in the comments I'd change the name of your variables to match the Java standards (camelCase for local variables and methods) , so to sum up :
while (!guessPass.equals(setPass)){
When comparing strings use String.equals("some string").
while(!guessPass.equals(setPass)){
//do something
}
I included changes to your variable names. Standard for variables is first word lowercase and all subsequent words uppercase (first letters). This will help you and your peers understand your code. Hope this helps.
Here is the fix for you. You need to use dot equal when comparing strings.
public static void main(String[] args) {
Scanner ScanObj = new Scanner(System.in);
System.out.println("Set Password: ");
String SetPass = ScanObj.nextLine();
System.out.println("Guess Password: ");
String GuessPass = ScanObj.nextLine();
// the fix
while (!GuessPass.equals(SetPass)){
System.out.println("Incorrect. Try Again: ");
GuessPass = ScanObj.nextLine(); }}
}