vendredi 2 décembre 2016

Storing string variables to an ArrayList, but the first gets skipped [duplicate]

Using a scanner to insert the string values and a wrapper class, however the scanner gets skipped on the first "Enter names: " of the for loop other than that it works

import java.util.ArrayList;
import java.lang.Integer;
import java.util.Scanner;

public class ArrayLists
{
  public static void main(String[] args)
  {
    Scanner scanner = new Scanner(System.in);

    ArrayList<Integer> intList    = new ArrayList<Integer>();
    ArrayList<String>  stringList = new ArrayList<String>();

    for(int i = 0; i < 5; i++)
    {
      System.out.println("Enter Integer:> ");
      int number = scanner.nextInt();
      Integer wrappedInt = new Integer(number);
      intList.add(wrappedInt);
    }

    for(int i = 0; i < 3; i++)
    {
      System.out.println("Enter names:> ");
      String names = scanner.nextLine();
      String wrappedString = new String(names);
      stringList.add(wrappedString);
    }

    System.out.println(intList);
    System.out.println(stringList);

  }
}




Aucun commentaire:

Enregistrer un commentaire