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
My program requires user to enter a 4-digit number(int type). Currently, when the user enters any character other than numeric, the program crashes. My hope is that when the user enters a non-numeric character, the program can tell the user "Wrong input" then let the user re-enter.
Does exception handling work for this? Or is there any other good way to solve this?
If you are using scanner, you can use the nextInt() method with the condition hasNextInt() which would only read integer inputs. Have a look at this post for example:
Taken from the above mentioned post.
Scanner sc = new Scanner(System.in);
System.out.print("Enter number 1: ");
while (!sc.hasNextInt()) sc.next();
int num1 = sc.nextInt();
int num2;
System.out.print("Enter number 2: ");
do {
while (!sc.hasNextInt()) sc.next();
num2 = sc.nextInt();
} while (num2 < num1);
System.out.println(num1 + " " + num2);
Also you can look at the documentation for scanner's nextInt() here
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 1 year ago.
Improve this question
so I am trying to use the scanner class to receive a few different inputs from a user. They can enter in a movie title, year, director, duration, actors, and genre.
However, for one of the user inputs, it is printing the director and duration together.
Here is a screenshot for more reference. Why is is asking for user input for director and duration at the same time but expecting a user input for duration?
Code and Output
System.out.println("Enter the title: ");
String title = myObj.nextLine();
System.out.println("Enter the year: ");
int year = myObj.nextInt();
System.out.println("Enter the duration: ");
int duration = myObj.nextInt();
System.out.println("Enter the director: ");
String director = myObj.nextLine();
System.out.println("Enter the actors: ");
String actors = myObj.nextLine();
System.out.println("Enter the genre: ");
String genre = myObj.nextLine();
int rating = ran.nextInt(10) + 1;
Calling nextInt() will not consume the newline. It will instead be consumed by the subsequent call to nextLine() when getting the director. To solve this you can instead call nextLine() and then Integer.parseInt() to convert the string to an integer.
.nextInt() will only read the integer provided in the input. Now the scanner cursor is at "/n"(newline). To avoid this you can either use
int duration = Integer.parseInt(myObj.nextLine());
or
int duration = myObj.nextInt();
myObj.nextLine();
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 1 year ago.
Improve this question
I know i have to use scanner and if condition but i don't know how to connect them
Example:
Are you willing to convert weight or height?
(user input= weight)
Will it be for imperial unit or metric unit?
(user input=metric)
Please provide the weight information in kg
(user input=75)
Your weight in kgs 75.00 is equal to 165.38 in pounds
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an input");
String input= scanner.next();
You can start by putting the user input into a variable. Depending on the flow of your program, you might need to parse the input into either integer or float.
int integerinput = Integer.parseInt(input);
float floatInput = Float.parseFloat(input);
From there, you can start doing the comparison. You have to be careful when parsing information, if the user entered a word and you try to convert that into either integer or float, you will get an exception.
You should get user’s input just once:
String answer = ask.nextLine();
And then inspect the input with if():
if (answer.equals("heght")){
// heght conversion goes here
}
else {
// weght conversion goes here
}
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 4 years ago.
Improve this question
I want to use a Class to hold user inputs. For example "example_name";"example_value";"example_city" is the form of the input and I want to make objects from an input stream of theese three inputs line by line from a file. So whenever I enter Adam;27; London it should be saved into an Object(Adam,27,London) and ask for the next input.
Thank you for your help, I know this might be a stupid question but I'm new to OO programming and I have a C background and don't want to use two dimensional arrays.
Let's break this problem into 3 section. First, we will have to design the class. Second, we will take the user input and create an object of that class until the user press n(no). Third, we will display the user information. Here I have used a LinkedList data structure for storing the inputs.
Section 1:
class example{
String example_name;
int example_value;
String example_city;
example(String name,int value,String city)
{
this.example_name = name;
this.example_value = value;
this.example_city = city;
}
}
section 2:
LinkedList<example> ob = new LinkedList<>();
Scanner input = new Scanner(System.in);
System.out.println("Press y to Continue n to Exit");
char i = input.next().charAt(0);
while(i!='n')
{
input.nextLine();
System.out.println("Enter name");
String name = input.nextLine();
System.out.println("Enter value");
int value = input.nextInt();
input.nextLine();
System.out.println("Enter city");
String city = input.nextLine();
ob.addLast(new example(name,value,city));
System.out.println("Press y to Continue n to Exit");
i = input.next().charAt(0);
}
Section 3:
for(example x:ob)
System.out.println(x.example_name + " " + x.example_value + " "+x.example_city);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I can't figure out why my code isn't working.
It appears to be breaking around the if not equal to yes or no area.
Here's my code:
public static void main(String[] args) {
// TODO code application logic here
Scanner user_input = new Scanner(System.in);
String name;
System.out.println("Hello, what is your name?");
name = user_input.next();
System.out.println("");
String name_answer;
System.out.println("Your name is " + name + ". Is this correct? (yes/no)");
name_answer = user_input.next();
System.out.println("");
if (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next();
while (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next(); } }
if (name_answer.equals("yes")) {
System.out.println("Thank you, " + name + ". Please proceed to the next question.");
} else if (name_answer.equals("no")) {
System.out.println("Please reinput your name correctly.");
while (name_answer.equals("no")) {
String name_again;
System.out.println("");
System.out.println("What is your correct name?");
name_again = user_input.next();
System.out.println("");
System.out.println("Your name is " + name_again + ". Is this correct? (yes/no)");
name_answer = user_input.next(); }
If i comment out the not-equals block of code (displayed below), the program works. However, with the block of code in, the program breaks.
if (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next();
while (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next(); } }
My goal is to have any answer not equal to "yes" or "no" be reinputted while a "yes" or "no" brings the program to another step. Thanks for the help.
The technical problem with your code is that you're using concatenation instead of logical operators. "yes" + "no" evaluates to "yesno", which will probably never match your input string.
More fundamentally, the problem is that you're trying to bundle two boolean evaluations into one. Logically, you want to proceed if the answer is not "yes" and the answer is not "no". In Java syntax:
if (!name_answer.equals("yes") && !name_answer.equals("no")) {
If you want to test multiple values at a time, you can use this shortcut:
if (!Arrays.asList("yes", "no", "foo", "bar").contains(name_answer)) {
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 6 years ago.
Improve this question
Hello i have been trying to make a program THAT ASK
1)How many students are there??
2)Asks a name and Test marks
can somebody help me with this . Im new to programming here is my code
Scanner input = new Scanner(System.in);
System.out.println("How many Students in class?");
int n = input.nextInt();
System.out.println("Enter the " + n + " names: ");
String [] names = new String[n];
for (int i = 0; i < names.length; i++)
{
names[i] = input.nextLine();
}
It is a "problem" with nextInt, the method only read the number and leave the <enter> so the next time you call nextLine, the <enter> being present, it will read this, so the value is empty. You need to clear the input first, simply by reading the line.
System.out.println("How many Students in class?");
int n = input.nextInt();
input.nextLine(); //will consume the \n