import java.util.*;
public class Test
{
static List<Integer> list = new LinkedList<Integer>();
public static void main(String[] args)
{
Scanner scanOne = new Scanner(System.in);
System.out.println("Enter integers to be added to the list with a space between every number : ");
String name = scanOne.next();
Scanner scanTwo = new Scanner(scanOne.nextLine());
while (scanTwo.hasNextInt())
{
list.add(scanTwo.nextInt());
}
print();
}
public static void print()
{
ListIterator<Integer> listIT = list.listIterator(); // using the list Interface's method "listIterator()" to Iterate through the listIT
while(listIT.hasNext())
{
int n = listIT.next();
System.out.print(n + " ");
}
}
}
import java.util.*; public class Test { static List<Integer> list = new LinkedList<Integer>(); public static
Share
You’ve made a simple error. You’ve called next() and then nextLine(), so the printing starts at the second element.