Able to input name for the first object alone [duplicate] - java

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
for(i=0;i<5;i++)
{
System.out.println("Enter name for student"+i);
stud[i].name=v.nextLine();
System.out.println("Enter number of the student"+i);
stud[i].regno=v.nextInt();
}
v is the scanner object. I am able to input name for only the 1st object in the array of object. when the loop accesses second object it directly asks me for the regno, not for the name!

Change to
System.out.println("Enter name for student"+i);
stud[i].name=v.next();

Related

Bug when taking mutliple input in while loop [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 1 year ago.
I get an error when i try to input name and num in a loop,
int i = 1;
while(i <3) {
System.out.print("Please enter name: ");
String name = input.nextLine();
System.out.print("Please enter number: ");
int num = input.nextInt();
i++;
}
The error that i am getting is this
on the first iteration the input is normal, however on the second iteration it prints the enter num and name at the same line. Could anyone explain to me why is this happening?
when calling nextInt() you only read the integer, but there is a new line character that should also be read.
so at the end of the loop, call input.nextLine() to read the new line.. and then the rest of the loop should work fine.
https://stackoverflow.com/a/26626204/14951486
Already answered
Refer the link
Error because you are taking String input after int

Scanner object doesn't read user input; java.util.NoSuchElementException: No line found [duplicate]

This question already has answers here:
Java -- Closing Scanner and Resource Leak
(3 answers)
Close a Scanner linked to System.in
(5 answers)
Closed 2 years ago.
I don't know why when I run this part of the code, it doesn't let me type in anything.
import java.util.Scanner;
public class IRA {
public void select() {
Scanner options = new Scanner(System.in);
System.out.println("What do you want to do: \na) Add cash \nb) Invest \nc) Transfer money \nd) Exchange currencies");
System.out.println("Please, enter a, b, c or d: ");
String letter = options.nextLine();
char c = letter.charAt(0);

Java difference [duplicate]

This question already has answers here:
Integer.parseInt(scanner.nextLine()) vs scanner.nextInt()
(4 answers)
Integer.parseInt(scan.next()) or scan.nextInt()?
(4 answers)
Closed 5 years ago.
Hi I'm just beginner in Java and I don't undestand one thing what's the difference between this two codes:
System.out.println("Write a number: ");
int a = Integer.parseInt(sc.nextLine());
System.out.println("Write a number: ");
int b = sc.nextInt();
In nextLine() if you input number values that it will consider as String.
Then it is parsed to Integer and stored in int a
In nextInt() the entered numerical value is considered Int and stored in int b

Java with parallel arrays [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 5 years ago.
I wrote some code off my Pseudo post and it compiles, but after entering the item name and cost the first time, then the name again it throws an error with input mis match exception. I'm not sure why.
import java.util.Scanner;
public class NewAssignment
{
public static void main(String args[]){
use names[count] = keyboard.next(); instead od names[count] = keyboard.nextLine(); it will work.

Java String Output Error Using Java.Util.Scanner [duplicate]

This question already has answers here:
Scanner issue when using nextLine after nextXXX [duplicate]
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 9 years ago.
I am writing a basic code which will be taking the number of question as input. After that I am using a for loop to Read each question. However, It is not asking for the first question and directly moving to the second question. Here is the code :
import java.util.Scanner;
class quiz
{
static Scanner in = new Scanner(System.in);
public static void main(String args[])
{
int numberofquestion,i;
String[] question;
question = new String[200];
System.out.print("Enter the number of Question : ");
numberofquestion = in.nextInt();
System.out.println("The number of question you entered is : "+numberofquestion);
for(i=0;i<=numberofquestion-1;i++)
{
System.out.print("Enter the Question : ");
question[i] = in.nextLine();
}
for(i=0;i<numberofquestion;i++)
{
System.out.print("Question = "+question[i]);
}
}
}
One solution is to add
in.nextLine();
after the in.nextInt();
Why? Because nextInt() doesn't read the "newline" character in the input, so your nextLine() in the loop was consuming it.

Categories

Resources