Is there a way to remove the error that i am getting?

Viewed 75
import java.util.ArrayList;
import java.util.*;

public class Solution { 
    public static void main (String args []){
        ArrayList<String> cars=new ArrayList<String>();

        Scanner sc =new Scanner (System.in);
        System.out.println(" enter the size of the list ");
        int size=sc.nextInt();
        

        for( int i =0;i<size;i++){ 
             
             cars.add(i,sc.nextLine());
     
        }
        System.out.println(cars);
        sc.close();
    }
}

This program should output

1,2,3,4

But if I enter the size as 4 and I enter these values only but instead, if I write the size as 4 ... it doesn't take 4 arguments from me instead it will take three and print(' ',1,2,3)

please help

4 Answers

The nextLine() traverse the pointer to the next line. So using next solves the problem

import java.util.ArrayList;

import java.util.*;
public class Solution { 
    public static void main (String args []){
       ArrayList<String> cars=new ArrayList<String>();

       Scanner sc =new Scanner (System.in);
        System.out.println(" enter the size of the list ");
        int size=sc.nextInt();
        

        for( int i =0;i<size;i++){ 
            System.out.println(" enter the " + i + " number : ");
            cars.add(i, sc.next());
     
        }
        System.out.println(cars);
        sc.close();
    }
}

You need to add sc.nextLine() just before the for loop.

When you enter a number then press Enter, input.nextInt() takes only the number, not the end of the line. However, when you execute input.nextLine(), it takes the end of the line which is already in the buffer from the first input. Always use input.nextLine() immediately after input.nextInt()

You were trying to accept a number but you have declared the ArrayList type as String. Your code will look like this

import java.util.ArrayList;

import java.util.*;
public class Main
{
    public static void main(String[] args) {
    
        ArrayList<String> cars=new ArrayList<String>();
        Scanner sc =new Scanner (System.in);
        System.out.println(" enter the size of the list ");
        int size=sc.nextInt();
        
        sc.nextLine();
        for( int i =0;i<size;i++){ 
             cars.add(i,sc.nextLine());
        }
        System.out.println(cars);
        sc.close();
    }
}

when you do list.add(i, value) what it does is keeps the value in the i index of the of List.
What .nextLine does it read the next line which which is blank, which resulting in *space* in the first position of the list.

so when you are doing cars.add(i,sc.nextLine()) this is adding space in the first index ie 0. What u can do is.

for( int i =0;i<size;i++){ 
      cars.add(i,sc.next());
    }

When you do sc.next() it check for the next character rather then Traversing to the nextLine.

There are a couple of ways to deal with this problem e.g.

  1. Input the size using Integer.parseInt(sc.nextLine())
  2. Put a sc.nextLine(); after int size=sc.nextInt();

The reason for this is that Scanner#nextInt does not consume the line-break which was entered by pressing Enter. The sc.nextLine() consumes this line-break.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // Declare the list
        List<String> cars = new ArrayList<>();

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the size of the list: ");
        int size = Integer.parseInt(sc.nextLine());

        for (int i = 0; i < size; i++) {
            cars.add(i, sc.nextLine());
        }

        System.out.println(cars);
    }
}

A sample run:

Enter the size of the list: 4
1
2
3
4
[1, 2, 3, 4]

A couple of more suggestions (as you can see in the code above):

  1. Use the Generic type i.e. List instead of ArrayList for the type of the variable e.g. List<String> cars=new ArrayList<>();. Also, note that you do not need to put <String> again on the right side; the compiler can infer it from simply <>.
  2. Do not close a Scanner for System.in as it also closes System.in and you will not be able to open it again unless you restart the application. There may be cases that your application has many classes and some other class in the application requires taking some input from the keyboard (System.in).
Related