This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 6 years ago.
I am trying to add elements to hashset, but it gets an empty element into it.
Initially I tried,
import java.util.*;
public class SetTrial{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
HashSet<String> names = new HashSet<String>();
for(int j=0; j<number;j++)
{
String text = sc.nextLine();
names.add(text);
}
System.out.println(names);
}
}
When I give input as,
5
a
b
c
d
e
It seems to only accept input till d and execute print displaying
[, a, b, c, d]
My guess was that it is accepting a newline at beginning, so I added a sc.next() in the code.
import java.util.*;
public class SetTrial{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
HashSet<String> names = new HashSet<String>();
sc.next();
for(int j=0; j<number;j++)
{
String text = sc.nextLine();
names.add(text);
}
System.out.println(names);
}
}
Although this time it seems to accept all of the input properly, the result is
[, b, c, d, e]
So the problem must be something else. How do I fix this?
Second approach was nearly right.
Just replace sc.next() with sc.nextLine().
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Scanner only reads first word instead of line
(5 answers)
Closed 4 months ago.
I am running this code to get 3 values: an integer, a string and a boolean from the user and print it in separate lines.
import java.util.*;
public class Practice {
public static void main(String[] args){
int a;
String b;
boolean c;
Scanner scanner = new Scanner(System.in);
a = scanner.nextInt();
b = scanner.nextLine();
c = scanner.nextBoolean();
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
I am trying to give input like this:
1
hello world
true
and am getting this error after writing the second line of input
next() can read the input only till the space. It can't read two words separated by a space. Also, next() places the cursor in the same line after reading the input.
nextLine() reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
String b = scan.next();
boolean c = scan.nextBoolean();
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
so basically you can use next() instead of nextLine() or reorder if you really need to use nextLine()
b = scanner.nextLine();
a = scanner.nextInt();
c = scanner.nextBoolean();
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
Why does this code print an extra line in the beginning, and also after accepting 2 strings it takes 0 to 2, which is 3 iterations to print the output?
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<=n;i++)
{ String s1="",s2="";
String str;
str= sc.nextLine();
int l=str.length();
for (int k=0;k<l;k++)
{
if (k%2==0)
s1=s1+str.charAt(k);
else if(k%2!=0)
s2=s2+str.charAt(k);
}
System.out.println(s1+" "+s2);
}
}
}
I tweaked your solution a bit. It should work.
Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
for(int i=0;i<n;i++)
{ String s1="",s2="";
String str;
str= sc.nextLine();
int l=str.length();
for (int k=0;k<l;k++)
{
if(k%2==0)
s1=s1+str.charAt(k);
else if(k%2!=0)
s2=s2+str.charAt(k);
}
System.out.println(s1+" "+s2);
}
sc.close();
Edit:
Also, you were iterating (n+1) times
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 4 years ago.
I was trying to take input for the number of names to be stored in an array from user and then using that i was taking names from the user ,first i tried to take names from the user using next() method and all the things were fine but when i tried to take input using nextLine() method the output was as shown below
package learningJava;
import java.util.Scanner;
public class practice
{
public static void main(String[] args)
{
int n;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the number of names you are gonna enter");
n = obj.nextInt();
String names[] = new String[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter the name of friend "+(i+1));
names[i]=obj.nextLine();
}
obj.close();
System.out.println("Names of your friends are");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}
}
Output for the nextLine() method
Enter the number of names you are gonna enter
5
Enter the name of friend 1
Enter the name of friend 2
It is not prompting me to enter the name of friend 1 and directly skipping it and coming to the friend 2 line.
I am beginner in Java , i know the basic difference in next and nextLine() that next() doesn't take input after a space but nextLine() takes complete input , So what is happening here ??
just in for loop, just change "println" to "print" because nextLine() consumes new line character.
import java.util.Scanner;
public class practice
{
public static void main(String[] args)
{
int n;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the number of names you are gonna enter");
n = obj.nextInt();
String names[] = new String[n];
for(int i=0;i<n;i++)
{
System.out.print("Enter the name of friend "+(i+1));
names[i]=obj.nextLine();
}
obj.close();
System.out.println("Names of your friends are");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}
}
check this answer: Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info?
This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 7 years ago.
I am writing a program that the main method asks the user for how many numbers they want in an array, then asks them to input that many numbers, and stores them in initialArray. In another method reverseTheArray, a NEW array is created that stores the elements of initialArray in reverse order. The method is called in main and prints the reversed array. No matter what input, the string "[I#33909752" is printed at the end. What is this and how do I get rid of it?
import java.util.Scanner;
public class Reverse {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("How many numbers do you want in your array?: ");
int num = scan.nextInt();
int[] initialArray = new int[num];
System.out.println("Please enter your numbers into the array: ");
for(int i=0; i<num; i++){
initialArray[i] = scan.nextInt();
}
System.out.println(reverseTheArray(initialArray));
}
public static int[] reverseTheArray (int[] initialArray){
int[] reversedArray = new int[initialArray.length];
for(int j=0; j<reversedArray.length; j++){
reversedArray[j] = initialArray[initialArray.length-1-j];
System.out.print(reversedArray[j]+" ");
}
return reversedArray;
}
}
You're seeing the default toString() from an array. To see the array items themselves you could use the java.util.Arrays toString(...) method like so:
System.out.println(java.util.Arrays.toString(reverseTheArray(initialArray)));
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
I am trying to create a simple String Revert program that does the following:
Prompts the user for an integer n
Creates an array of n Strings
Keeps reading character strings from user and stores them in the array, until the end of the array or user types "quit"
Prints the strings from the array in reverse order excluding empty slots
Here is my attempt so far:
-However, when i take input and make the size 4, the buffer only reads 3 strings and stops rather than 4.
import java.util.*;
import java.io.*;
class StringRevert {
public static void main(String[] args) {
String myArray[];
Scanner Scan = new Scanner(System.in);
System.out.println("Enter Number: ");
int size = Scan.nextInt();
myArray = new String[size];
for(int i=0; i<myArray.length; i++) {
myArray[i] = Scan.nextLine();
}
}
}
You need to put Scan.nextLine(); before starting of for loop.
public static void main(String[] args) {
String myArray[];
Scanner Scan = new Scanner(System.in);
System.out.println("Enter Number: ");
int size = Scan.nextInt();
myArray = new String[size];
Scan.nextLine();
for(int i=0; i<myArray.length; i++) {
System.out.println("Enter String");
myArray[i] = Scan.nextLine();
}
}