I am having trouble figuring out how to read input from the keyboard and inserting that into a LinkedList. Any help or suggestions would be greatly appreciated.

import java.util.*;
import java.util.Scanner;
import java.io.*;

public class listlist{

        public static void main(String [] args){

             LinkedList ll = new LinkedList();
             Scanner in = new Scanner(System.in);
             System.out.println("Enter a number: ");
             int n = in.nextInt();

             ll.add(n);

        }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I am having trouble figuring out how to read input from the keyboard and inserting that into a LinkedList. Any help or suggestions would be greatly appreciated.

import java.util.*;
import java.util.Scanner;
import java.io.*;

public class listlist{

        public static void main(String [] args){
                 
             LinkedList ll = new LinkedList();
             Scanner in = new Scanner(System.in);
             System.out.println("Enter a number: ");
             int n = in.nextInt();

             ll.add(n);

        }
}

I think your program is working fine it is adding number to the linked list if you want to add more than one number try using some sort of loop.
if you are trying to do something else let me know.

Edited 7 Years Ago by sfar_furqan: n/a

How can I alter what I have now so that It will continue to read user input until the user types:

-1

and then it will stop?

[code=java]import java.util.*;
import java.util.Scanner;
import java.io.*;

public class ListList{

	public static void main(String [] args){

		LinkedList ll = new LinkedList();	
		Scanner in = new Scanner(System.in);
		int n = 0;
		while (!(n<0)){
			System.out.println("Enter a number: ");
			n = in.nextInt();

			ll.add(n);
		}
		System.out.println(ll);

	}
}

Try this and tell me if that is what you are trying to do.

Indianboi: by reading it as a String and using the split method.
Check the String api for the split method.

For future reference, please don't revive dead threads. This thread is over five years old, if you have a new question, ask it in a new Thread.

This article has been dead for over six months. Start a new discussion instead.