1

I am trying to make a three different linked list. I will determine the first ones inputs but for the other two I want to ask the user for the inputs and then insert them into a linked list. Can anyone help me with how to do that? So far I could only write this code

package homework001;

 import java.util.Scanner;
 import java.util.List;
 import java.util.LinkedList;
 import java.util.ListIterator;

public class morph {

    public static LinkedList<String> list;
    public static void main(String[] args){
        LinkedList<String> list = new LinkedList<>();
        list.add("10");
        list.add("34");
        list.add("1");
        list.add("97");
        list.add("5");
        list.add("62");     

   }
 }
4
  • use a Scanner to take the user input. How many times do you want the user to keep entering data? Commented May 5, 2017 at 19:00
  • If you google "java user input from console" you'll find plenty of tutorials about using Scanner Commented May 5, 2017 at 19:01
  • Yes I used Scanner but after that how am I gonna insert all the data that I've got into Linked List that is my rela question Commented May 5, 2017 at 19:03
  • @LeenaliLi show the code which is using the scanner then. ^^. Commented May 5, 2017 at 19:05

4 Answers 4

2

I think we can simply take user input in LinkedList by using this method -> listname.add(sc.nextInt());

code for the implementation is below! thank you :)

public class LL_userInput {

    public static void main(String[] args) {

        LinkedList<Integer> ll = new LinkedList<>(); //creating list
        
        Scanner sc = new Scanner(System.in); //creating scanner for total elements to be inserted in list
        System.out.println("enter total count of elements -> ");
        int num = sc.nextInt(); // user will enter total elements
        
        while(num>0) { 
        ll.add(sc.nextInt());
        num--;  // decrement till the index became 0    
        }
        sc.close();
        System.out.println(ll);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Using scanner, you can get input from any source. To read from console use

Scanner sc = new Scanner(System.in);
while(!sc.hasNextInt()) sc.next();
int number = sc.nextInt();

for(i=0; i< number; i++)
    myList.add(sc.next());

Comments

0

I think you don't understand from the comments here is a simple example ;

public static void main(String[] args) {
    LinkedList<String> list = new LinkedList<>();//declare your list
    Scanner scan = new Scanner(System.in);//create a scanner
    System.out.print("Enter the Nbr of element : ");
    int nbr = scan.nextInt();//read the number of element
    scan.nextLine();
    do {
        list.add(scan.nextLine());//read and insert into your list in one shot
        nbr--;//decrement the index
    } while (nbr > 0);//repeat until the index will be 0

    scan.close();//close your scanner
    System.out.println(list);//print your list

}

1 Comment

Thanks. I guess I didn't at first. I'm a beginner. Sorry for trouble.
0
import java.util.*;
class LinkedList{
public static void main(String[] args) {
    Scanner sc= new Scanner (System.in );
    LinkedList<Integer>list=new 
LinkedList<>();
    System.out.println("Enter how many 
elements you want");
    int num=sc.nextInt();
for(int i=0;i<num;i++){
       System.out.println("Enter element 
at index "+i);
list.add(sc.nextInt());
   }
   
 System.out.print(list+" ");

}
}

1 Comment

It's very simple code to understand as beginners

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.