-1

I have created my linked list and I wish for the user to enter a Station and then the output is the number stored for that station.

    LinkedList myList = new LinkedList();



            myList.addFirst("London", 5);            
      myList.addNode("Manchester ", 10);

      myList.addNode("Liverpool", 20);
      myList .addNode("Birmingham", 50);

This is the input for the user to enter.

          String name;              
              name = JOptionPane.showInputDialog("Enter Station: ");


   StringNode  temp;

       temp = mylist.head;

       if (temp.Station == (name)) {


           System.out.println("Yes");


       }

The rest of the methods are just adding a new data and printing.

Thanks

1 Answers1

1

This problem is what a HashMap is for.

Map<String, Integer> map = new HashMap<>();
map.put("London", 5);            
map.put("Manchester ", 10);
map.put("Liverpool", 20);
map.put("Birmingham", 50);

String station = "Liverpool";
Integer i = map.get(station);
System.out.println(i);
pscuderi
  • 1,554
  • 12
  • 14