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!?
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 4 hours ago.
Improve this question
I'm trying to make my program work with a new input instead of the old one when the user clicks an option from the options menu
I don't what the problem that is not causing the program to continue with the new input instead of the old one. thanks in advance.
public static String optionSix (String input)
{
String newInput;
Scanner scnr = new Scanner (System.in);
System.out.println("Enter List of Words Seperated by Spaces: ");
newInput = scnr.nextLine();
input = newInput;
return input;
}
Java is pass-by-value, so you can't override a string's value like that. You'll have to assign the new value to it:
public static void main(String args[]) {
String someOldString = "some old value";
someOldString = optionSix(); // No need to pass the original value either
}
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 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.
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 8 years ago.
Improve this question
I am trying to validate input values to pass only integers that are divisible by 10. The code below is failing.
public static void main(String args[]) {
Scanner scan =new Scanner(System.in);
ArrayList<Integer> liste = new ArrayList<Integer>(); // I have filled my array with integers
int x=scan.nextInt();
int y=x%10;
do{
if(y==0){
liste.add(x);}
else if(y!=0){
System.out.println("It is not valid"); continue;
}
else
{System.out.println("Enter only integer"); continue;
}
}while(scan.hasNextInt()); }
System.out.println(liste);
System.out.println("Your largest value of your arraylist is: "+max(liste));
You're calling scan.nextInt() twice. Each time you call it, it will read another int from the input. Thus, if your input was something like
10
5
13
then the 10 would pass the scan.nextInt()%10==0 check, and then 5 would be added to the list. Store the result of scan.nextInt() in a variable first, so the value won't change.
Instead of
if(scan.nextInt()%10==0){
liste.add(scan.nextInt());}
do
int num = scan.nextInt();
if(num%10 == 0){
liste.add(num);
}