I am doing a project for school and my job is to make a LinkedList program that a user can read into a line of ints and use the Linked List functions to print them in reverse.
I have it somewhat working, however, every time I must end the read in from System.in by pressing Ctrl+C. Every... Time...
I'm trying to find a way to stop the scanner read when it reads the int -1.
Whitespace is okay as well. But once it reads -1, i want it to stop
But I cannot find the proper way.
Here is what I have so far:
public class ReverseUsingLinkedList
public static void main(Stringargs)
System.out.print("Enter a sequence of Integers, -1 to end: ");
LinkedList<Integer> num= new LinkedList<Integer>();
Scanner keyboard = new Scanner(System.in);
num.removeFirst(); //Removes the -1 from the LinkedList
System.out.println("List in reverse :" + num.toString());
to test if input is
-1 and to break if so
while(keyboard.hasNext()) int num = keyboard.nextInt(); if (num == -1) // or less than 0 ? break; num.addFirst(num); }
Please note @nullpointer very valid comment