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 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.

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.

how can u do this if the input is space seperated.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.