User Input of sentence in Java [duplicate] - java

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
Whenever I use sc.nextLine in java I get this error. It skips one line. Anybody knows how to fix it. sc.nextLine fails for multiple test cases as takes skips one line and then take input. Anybody knows another way to take sentence input in java?
For Input:
2
geeksforgeeks
geeks for geeks
your output is:
geeksforgeeks
Code:
class GFG {
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
while(testcases--!=0)
{
String str=sc.nextLine();
System.out.println(str);
}
//code
}
}

you have to read a line after int testcases=sc.nextInt(); because reading the Int from scanner is not reading a line Termination chars....
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int testcases=sc.nextInt();
sc.nextLine(); //<--HERE!
while(testcases--!=0)
{
String str=sc.nextLine();
System.out.println(str);
}

Related

how to take multiple input using NextLine() in java [duplicate]

This question already has answers here:
What's the difference between next() and nextLine() methods from Scanner class?
(15 answers)
Closed 5 years ago.
how to take more inputs from user using Nextline() in java
if i use scn.Nextline() after reading one input it will work but giving a null in answer.
public class Mapdemo {
public static void main(String[] args) {
Scanner scn =new Scanner(System.in);
System.out.println("enter the number of phoneaddress you want to store");
int n=scn.nextInt();
int j=0;
System.out.println("enter the phonenumber first and then name of the person phoneaddress");
Map<String, Long> m1=new HashMap<String,Long>();
for(int i=0;i<n;i++)
{
Long l=scn.nextLong();
String s=scn.nextLine();
m1.put(s,l);
}
System.out.println("enter the name to be checked");
String s2=scn.next();
System.out.println(m1.get(s2));
After using .nextInt() for the first time do:
scanner.nextLine();
after that to clear the line, nextLine() stops at the newline character (OS dependant).
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()

Program runs infinitely when inputting a multi-line String in JAVA (BlueJ) using Scanner [duplicate]

This question already has answers here:
Why does hasNextLine() never end?
(5 answers)
How to terminate Scanner when input is complete?
(8 answers)
Closed 5 years ago.
I wrote a program in BlueJ that accepts a multi-line String and its job is to print that String as a single-line continuous String.
Here's the code I wrote -
import java.util.Scanner;
class test
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter String: ");
String s = sc.nextLine();
while(sc.hasNextLine())
{
s+=sc.nextLine();
}
System.out.println(s);
}
}
When I run this program and give the following input by copy-pasting it into the BlueJ terminal -
10100101111011111111
00000000000000000000
01011101110110101111
the program runs infinitely and gives no output.
To check where the code was going wrong I changed it to -
import java.util.Scanner;
class test
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter String: ");
String s = sc.nextLine(), a = s;
while(sc.hasNextLine())
{
System.out.println(s);
s+=sc.nextLine();
}
System.out.println(s);
}
}
However, the output I'm getting is -
10100101111011111111
1010010111101111111100000000000000000000
and after that the program runs infinitely without any output.
Can anyone fix my code so that it gives the proper output -
101001011110111111110000000000000000000001011101110110101111

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.

Scanner -NoSuchElementException [duplicate]

This question already has answers here:
Scanner NoSuchElementException
(2 answers)
Closed 7 years ago.
I am executing the below code to read a string from the input stream with the scanner object.
public class Test{
public static void main(String[] args) {
String userInput1 = getUserInput();
System.out.println(userInput1);
String userInput2 = getUserInput();
System.out.println(userInput2);
}
private static String getUserInput() {
System.out.println("Enter the String");
Scanner scanner = new java.util.Scanner(System.in);
String input = scanner.next();
scanner.close();
return input;
}
}
The first invocation to the method getUserInput succeeded without any issues.But he second invocaton threw NoSuchElementException.
Enter the String
test1
test1
Enter the String
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
There are two problems with the code
It assumes that there already has been input. A simple while(!scanner.hasNext()){} before the locations where you call scanner.next(); should work.
You are creating many scanners (this may or may not impact the code, but does impact efficiency)
Instead, the Scanner scanner = new Scanner(System.in); can be broken up into a declaration (Scanner scanner;) outside a method and then defined (scanner = new Scanner(System.in);) in the main method or initialization code.

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