Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Scanner scanner = new Scanner(System.in);
System.out.println("1. Do you like computers?");
String input = scanner.nextLine();
if (input.equals("no")) {
System.out.println("You should!");
if (input.equals("yes")){
System.out.println("I like computers too!");
If the user inputs anything else than "yes" or "no", how would i repeat the question til the user inputs "yes" or "no". I am new to java so detailed explanations will help.
Going off of the code nhouser9 provided (which is case-sensitive), one way to check for all variants ("Yes", "YES", "yes") is to use the .toUpperCase() method.
String input = "";
while (!input.toUpperCase().equals("YES") && !input.toUpperCase().equals("NO")) {
input = scanner.nextLine();
}
String input = "";
while (!input.equals("Yes") && !input.equals("No")) {
input = scanner.nextLine();
}
The above should loop until the user has entered a valid input. Note that the input here is case sensitive - you can add more ifs to check for other cases if you need to.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 days ago.
Improve this question
public static String reqnumber() {
Scanner keyboard = new Scanner(System.in);
String s;
boolean isMatch = true;
ArrayList<String>patientsNumbers = new ArrayList<String>();
int i = patientsNumbers.size();
do
{
System.out.println("enter number: ");
s = keyboard.next();
if(!(patientsNumbers.contains(s))){
patientsNumbers.add(s);
isMatch = false;
}else{
System.out.println("this is a number taken by another patient");
}
}while(isMatch);
return patientsNumbers.get(i);
}
here is a piece of code that includes an input validation loop, as every number of patient entered by user it should be saved in an arraylist called patientsNumbers.
and when the method reqnumber() is called in main, in the output when a user inputs a number already exist it should prompt the user that the number already taken by another patient and to enter another number but it doesn't, how to solve this!?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I want to Make a game Enter ATM pin with only 5 chances if you guess the correct pin it will stop automatic, and if Chances = 0 it will stop also,
int answer, chances = 5;
do{
Scanner scan = new Scanner(System.in);
System.out.print("Enter your Bank ATM pin : ");
answer= scan.nextInt();
chances--;
}while(chances > 0);
}
You can do something like that:
int answer, chances = 5;
boolean authorized=false
do{
Scanner scan = new Scanner(System.in);
System.out.print("Enter your Bank ATM pin : ");
answer= scan.nextInt();
if(answer.equals("ok")){
authorized=true;
}else{
chances--;
}
}while(chances > 0 || authorized);
Code Safe
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Here is the code I have.
//input
System.out.println("Using numbers, in what month were you born? ");
String **userMonth** = input.nextLine();
// also tried int **userMonth** = input.nextInt();
It does not work either way.
userMonth will not "activate?" (sorry noob and don't know all terms)
When tried to call later in program error:
if (userMonth < 3) {
System.out.println("Your vacation home is a van down by a river!");
}
//userMonth cannot be resolved to variable
Very odd that your variables are showing up as **varName** instead of varName.
First use:
int userMonth = 0; // initialize variable.
userMonth = input.nextInt();
But it does not consume the newline ('ENTER').
Therefore, immediately after, consume the newline character via:
input.nextLine();
Also make sure Scanner was declared as
Scanner input = new Scanner(System.in);
Don't forget to close the scanner at the end of program
input.close();
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to delete the user integer input from an array. However, I can't get the value of the input.
while(keepGoing)
{
while (!scan.hasNextInt() )
{
scan.next();
System.out.print('\n'+"Choose a valid number: ");
}
int unitA = scan.nextInt();
if (unitA < 1)
{
System.out.print('\n'+"Choose one of the options: ");
keepGoing = true;
}
else if (unitA > 14)
{
System.out.print('\n'+"Choose one of the options: ");
keepGoing = true;
}
else
lengthValue.remove(unitA);
scan.close();
keepGoing = false;
}
//lengthValue.remove(int unitA);
System.out.println(unitA);
In my opinion, you forget to press "Enter" after entering input. A scanner can read the input if only you press the "Enter" key. Your solution seems correct to me. I was able to properly run it on my PC.
You can find a similar question here:
How to use Scanner to accept only valid int as input
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
So our professor was making us do a loop where in we will ask a question if we want to continue, and if the user inputs "Y" it will ask again and so on.. But once the user inputs "N", it will say "Good Bye!".. I tried using the if statement and when the user inputs "N" it will ask again, it didn't end with "Good Bye!". So after stumbling upon here and managed to get it work.. I just wanna ask, why do I have to put input.equals("Y"); inside the loop statement?
Thank you in advance!
import java.io.*;
import java.util.*;
public class While{
public static void main(String Args[]){
String input;
String b="Y";
String c="N";
Scanner get=new Scanner(System.in);
System.out.println("Please enter 'Y' for YES and 'N' for NO.");
System.out.print("Do you want to continue? ");
input = get.nextLine().toUpperCase();
while (input.equals(b))
{
input.equals("Y");
System.out.print("Do you want to continue? ");
input = get.nextLine().toUpperCase();
}
System.out.println("Good Bye!");
}
}
input.equals("Y") just checks whether input equals "Y" and returns true if it does, false otherwise. But since you do not use the return value of .equals at all, this line is completely useless.