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();
Related
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 3 years ago.
Improve this question
Java program to find a character from a sentence and replace it with another character. If the character is not found in the string print "character not found".
Note: Replace only the first occurrence.
Sample input 1:
Enter the string:
java programming
Enter the character to be searched:
a
Enter the character to replace:
o
Sample output 1:
jova programming
Kindly suggest me how to take user input for the character to be replaced and replace the character.
In general to get a input from user you could a scanner class.
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter the character to be searched");
String characterToReplace = myObj.nextLine(); // Read user input
System.out.println("Enter the character to replace");
String replacementCharacter = myObj.nextLine();
Information on scanner class
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
Other ways to read input from command line :
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
For replacing a character you could just string.replace method to perform the required operations.
You can achieve this by putting this code in a function:
Scanner readInput = new Scanner(System.in);
System.out.println("Enter the string to search:");
String search = readInput.nextLine();
System.out.println("Enter the character to be searched:");
String find = readInput.nextLine();
System.out.println("Enter the character to replace it with:");
String replace = readInput.nextLine();
if (search.contains(find)) {
return search.replaceFirst(find, replace);
} else {
return "Character not found";
}
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 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
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