0

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 by mike_2000_17: Fixed formatting

4
Contributors
5
Replies
16
Views
7 Years
Discussion Span
Last Post by stultuske
0

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 by sfar_furqan: n/a

0

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?

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

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.