Java with parallel arrays [duplicate] - java

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.

Related

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. First loop doesnt work [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
i got a little problem with my code. When i write "exit" still shows statement "Unknown command" and i want to show "Bye". Can you help me?
import java.util.Scanner;
public class Hello{
public static void main(String[] args){
Scanner odczyt = new Scanner(System.in);
String word;
do{
word = odczyt.nextLine();
System.out.println("Unknown command");
}
while(word!="exit");
System.out.println("Bye");
}
}
"Unknown command" will always be printed. Besides that you shouldn't use = to compare Strings. You should use .equals() or .equalsIgnoreCase().

How to get a whole line of input using scanner [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
I know a lot of people have asked this but everyones answer is to use scanner.nextLine() however this does not work for me. I have this code:
if (option == 1) {
System.out.println("What is the name of the goal the task is under?: ");
String goalName = scanner.next();
}
When I use .next() I can only get one word, however when I use .nextLine() the command line does not allow for the user to input anything so it asks "What is the name of the goal the task is under?:" and then continues on the program without letting input occur.
The following test code works:
package asdfsadf;
import java.util.Scanner;
public class Asdfsadf {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("What is the name of the goal the task is under?: ");
String goalName = scanner.nextLine();
System.out.println("You entered: ");
System.out.print(goalName);
}
}
So the only problem would be if you used scanner before this say you had scanner.nextInt() and that would leave an empty "end of the line" in the scanner. So the nextLine would take the end of the line from the previous call. To fix this, just create a new scanner object.

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

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();

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