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();
}
}
Related
This question already has answers here:
How to append elements at the end of ArrayList in Java?
(4 answers)
Closed last year.
public void getDisMarks()
{
marks=new int[3];
System.out.print("Enter marks of Physics: ");
marks[0]=sc.nextInt();
System.out.print("Enter marks of Chemistry: ");
marks[1]=sc.nextInt();
System.out.print("Enter marks of Maths: ");
marks[2]=sc.nextInt();
}
So in this piece of code we are using array for 3 definite subjects. And we're using scanner class to input from the user. Let's say in the future I want to add a couple of more subject. So coding it again would not make it any flexible.
So I read that we could use arrayList, How can I use scanner class with arrayList similar to this piece of code.
You could do something like this:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> list = new ArrayList<>();
System.out.println("How many marks to enter?");
int marksToEnter = sc.nextInt();
for (int i = 0; i < marksToEnter; i++) {
System.out.println("Enter next mark");
list.add(sc.nextInt());
}
System.out.println(list);
}
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 exists:
Scanner issue when using nextLine after nextXXX [duplicate]
Closed 8 years ago.
Why is my while loop making two passes before allowing user input? Tried the same with for loop and can't figure out why it's asks for input twice before allowing user to enter input. I know it's a stupid, simple logic mistake I'm making but I'm a noob. Thanks in advance.
public static void main (String [] args){
Scanner scan = new Scanner(System.in);
System.out.println("How large would you like the array to be? (number)");
int arraySize = scan.nextInt();
String [] myArray = new String [arraySize];
int i = 0;
if (arraySize <= 0 ) {
System.out.println("Please enter a positive integer for the array size. Rerun program when ready.");
} else {
while (i < myArray.length) {
System.out.println("Please type a string to be entered in the array");
myArray[i] = scan.nextLine();
i++;
}
}
}
Output looks like
How large would you like the array to be? (number)
5
Please type a string to be entered in the array
Please type a string to be entered in the array
Add scan.nextLine(); right after your nextInt:
int arraySize = scan.nextInt();
scan.nextLine();
The first iteration of the while loop is not blocking on the nextLine() because it is picking the new line after the first integer that you input.
Try this:
public static void main (String [] args){
Scanner scan = new Scanner(System.in);
System.out.println("How large would you like the array to be? (number)");
int arraySize = scan.nextInt();
scan.nextLine(); // This advances the Scanner to the next line.
String [] myArray = new String [arraySize];
int i = 0;
if (arraySize <= 0 ) {
System.out.println("Please enter a positive integer for the array size. Rerun program when ready.");
} else {
while (i < myArray.length) {
System.out.println("Please type a string to be entered in the array");
myArray[i] = scan.nextLine();
i++;
}
}
}