So basically I have created a linked list which allows me to enter numerical data into a list and for it to be sorted, added to and deleted but I need to add text into the list as well but not sure how to go about it I've tried changing integer into string but cant seem to get it to work.

this is my file List.java

import java.io.*;
public class List {
    public static Listnode head;
    public List() {
        head = new Listnode(0);
    }
    public static int readValue( )throws IOException {
       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
       String input;
       int value =0;
       boolean OK = false;
       while (!OK){
          try{
            input = in.readLine();
            value = Integer.parseInt(input);
            OK = true;
            }
         catch (Exception e) {
            System.out.println("Invalid - please enter integer value");
          }
        }
      return value;
    }

    public static boolean isEmpty() {
        if (head.next == null)
            return true;
        else
            return false;
    }


    public static void insert() throws IOException {
       Listnode current, previous, temp;
       int value;
       System.out.print("Enter number to insert, 0 if no more: ");
       value = readValue();
       while (value != 0) {
          temp = new Listnode(value);
          current = head;
          previous = head;
          while ((current.next != null) && (value > current.value)) {
              previous = current;
              current = current.next;
            }
           if (value < current.value) {
               temp.next = current;
               previous.next = temp;
            } else
                current.next = temp;
           System.out.print("Enter number to insert, 0 if no more: ");
           value = readValue();                 }

      }

    public static void search() throws IOException {
        Listnode current = head.next;
        int sought;
        boolean found = false;
        if (isEmpty())
            System.out.println("List is empty ");
         else {
            System.out.print(" Value sought: ");
            sought = readValue();
            while ((!found) && !(current == null)) {
               if (current.value == sought) {
                   System.out.println(sought + " is in the list ");
                   found = true;
                 }
                current = current.next;
              }
             if (!found)
               System.out.println("Item is not in the list ");
        }
    }

    public static void deleteItem() throws IOException {
           Listnode current, previous, temp;
           int sought;
           boolean found = false;
           if (isEmpty())
               System.out.println("List is empty ");
           else {
            System.out.print("Item to delete: ");
            sought = readValue();
            current = head;
            previous = head;
            while (!found && !(current == null)) {
                if (current.value == sought) {
                   previous.next = current.next;
                   System.out.println(sought + " deleted from list ");
                   found = true;
               }
              else {               
                  previous = current;
                  current = current.next;
                 }
            }
            if (!(found))
                System.out.println("Item is not in the list ");
          }

        }

    public static void writeList() {
        Listnode current = head.next;
        if (isEmpty())
            System.out.println("List is empty ");
        else {
            while (current != null) {
                System.out.println(current.value);
                current = current.next;
            }
        }
    }
}

This is my Listnode.java file

public class Listnode {
    int value;
    Listnode next;
    public Listnode(int data) {
    value = data;
    next = null;
    }
}

Listtest.java file

import java.io.*;

public class Listtest {
    public static int readValue( )throws IOException {
       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
       String input;
       int value =0;
       boolean OK = false;
       while (!OK){
          try{
            input = in.readLine();
            value = Integer.parseInt(input);
            OK = true;
            }
         catch (Exception e) {
            System.out.println("Invalid - please enter integer value");
          }
        }
      return value;
    }

    public static void main(String[] args) throws IOException {
        List mylist = new List();
        int choice = 7;
        System.out.println("Start by adding some items to the list...");
        mylist.insert();
        while (choice != 0) {
            System.out.println(
                    "------------------------------------------");
            System.out.print(
                    "1..Write out list  2..Insert items 3..Search 4..delete 0..Quit: ");
            choice = readValue();
            if (choice == 1)
                mylist.writeList();
            else if (choice == 2)
                mylist.insert();
            else if (choice == 3)
                mylist.search();
            else if (choice == 4)
                mylist.deleteItem();

        }
    }
}

So need to be able to add text into list as well so inserting into list wont just be "0800" but can be " 0800 Berlin" or something along those lines but I still need the wrtie out list, insert items, search and delete functions

The only variable in a ListNode is a int. If you want it to contain a String also, you will need to add another variable to the ListNode class that is type String.
Another way might be to use inheritance. Define a base class with the next node and extend the class with two classes, one for int and one for String. Not sure how sorting a list with the two different types of node would work.

Do you mean each node can contain an int and a String, or that each node can contain an int or a String, or do you mean you want to have two types of lists, one containing only ints and the other containing only Strings? (Each of those has a different solution.)

sorry for any confusion i want each node to contain an int and a string

In that case (as Norm said) you will need to replace the value int by two variables (one int one String) in the ListNode class and then go through all your methods updating them to allow for the fact that each node now has two values to enter/print/etc. It's a bit tedious to do, but not difficult.
However
If there's any risk that ant some later stage you say "... and a third value..." then maybe you should go straight to the general solution, which is to have a single value which is an Object, and create little classes to hold all the ints. STrings, floats etc that you want to store in each node.

so in order to change my code I need to do this?

public class Listnode {
    int value;
    string value;

    Listnode next;
    public Listnode(int data) {
    value = data;
    next = null;
    }
}

is that right? is there anything else with the code i need to do

You'd need a way to set the contents of the String variable.
Did you try compiling that code? What errors do you get?

when trying to complie the Listnode file now after changing to what I said it come up with

Listnode.java:3:cannot resolve symbol
symbol: class string
location: class Listnode
string value;

Listnode.java:3: value is already defined in Listnode
string value;

Do you understand why you are getting those errors?
Java is case sensitive so you must spell class names correctly.

value is already defined

You can not define 2 variables with the same name.

Edited 4 Years Ago by NormR1

oh i didnt think that i did call the variables the same thing, I'm just so stuck in it that i dont know which way is up! any help to help me see the light at the end of the tunnel would be appreciated

Did you see the spelling error? Change the spelling to use the correct name.

Did you see that there was a variable that was defined twice? Change the name of one of them.

Edited 4 Years Ago by NormR1

so is this right if i change it to this

public class Listnode {
    int value;
    string location;

    Listnode next;
    public Listnode(int data) {
    value = data;
    next = null;
    }
}

Ask the compiler first before posting, Then you can post the error messages with the code.

You need to post the full text of any error messages. What you posted does not have the name of the symbol or what line it was on.

sorry, it says

Listnode.java:3: cannot resolve symbol
symbol: class string
location: class Listnode
string location;

1 error

What is string? Isn't that the same error you posted before? Did you read my explanation that java requires correct spelling of the class names. There is no class named string. Check the API doc to get the name of the class that you are trying to use.

i thought String was a class name? or am i totally confused? I need real help and guidence on this feel as though I'm banging my head against a brick wall!

String is a class in the java SE packages. string is not. Java is case sensitive: s != S

Ok I changed that and its all compiling in jdk fine now but its still not allowing me to enter integer what else do I need to do now that i've declared a string as well

not allowing me to enter integer

Please post the error messages or the contents of the console that shows the problem.

well there are no error messages but its still displaying the line
Invalid - please enter an integer value
where it is input into my code and therefore not allowing me to put "paris" in but still allowing me to put "800" in

Where in the code does that message come from? Why is the code that displays that message executed?

Add some println statements to the code to show the value ot the variable that is causing the exception.
Add a call to the printStackTrace() method in the catch block to get the full text of the error message.

Edited 4 Years Ago by NormR1

its in this section of code here

 public static int readValue( )throws IOException {
       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
       String input;
       int value =0;
       boolean OK = false;
       while (!OK){
          try{
            input = in.readLine();
            value = Integer.parseInt(input);
            OK = true;
            }
         catch (Exception e) {
            System.out.println("Invalid - please enter integer value");
          }
        }
      return value;
    }

from the List.java file but I don't know how to change anything!

Print out the value of input
and add a call to the printStackTrace() method in the catch block

Edited 4 Years Ago by NormR1

where would I add that to after the

Ok = true; 

or after

 catch (Exception e) {

Do the println of input immediately after its value is set by the assignment: input = .... on line 8
Put the call to printStackTrace() inside the catch block after line 12

Edited 4 Years Ago by NormR1

this is my code now

import java.io.*;
public class List {
    public static Listnode head;
    public List() {
        head = new Listnode(0);
    }
    public static int readValue( )throws IOException {
       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
       String input;
       int value =0;
       boolean OK = false;
       while (!OK){
          try{
        input = in.readLine();
        value = Integer.parseInt(input);
        OK = true;
        }
        catch (Exception e){
            printStackTrace();
           System.out.printLn("Invalid");
}

        }
      return value;
    }

    public static boolean isEmpty() {
        if (head.next == null)
            return true;
        else
            return false;
    }


    public static void insert() throws IOException {
       Listnode current, previous, temp;
       int value;
       System.out.print("Enter number to insert, 0 if no more: ");
       value = readValue();
       while (value != 0) {
          temp = new Listnode(value);
          current = head;
          previous = head;
          while ((current.next != null) && (value > current.value)) {
              previous = current;
              current = current.next;
            }
           if (value < current.value) {
               temp.next = current;
               previous.next = temp;
            } else
                current.next = temp;
           System.out.print("Enter number to insert, 0 if no more: ");
           value = readValue();                 }

      }

    public static void search() throws IOException {
        Listnode current = head.next;
        int sought;
        boolean found = false;
        if (isEmpty())
            System.out.println("List is empty ");
         else {
            System.out.print(" Value sought: ");
            sought = readValue();
            while ((!found) && !(current == null)) {
               if (current.value == sought) {
                   System.out.println(sought + " is in the list ");
                   found = true;
                 }
                current = current.next;
              }
             if (!found)
               System.out.println("Item is not in the list ");
        }
    }

    public static void deleteItem() throws IOException {
           Listnode current, previous, temp;
           int sought;
           boolean found = false;
           if (isEmpty())
               System.out.println("List is empty ");
           else {
            System.out.print("Item to delete: ");
            sought = readValue();
            current = head;
            previous = head;
            while (!found && !(current == null)) {
                if (current.value == sought) {
                   previous.next = current.next;
                   System.out.println(sought + " deleted from list ");
                   found = true;
               }
              else {               
                  previous = current;
                  current = current.next;
                 }
            }
            if (!(found))
                System.out.println("Item is not in the list ");
          }

        }

    public static void writeList() {
        Listnode current = head.next;
        if (isEmpty())
            System.out.println("List is empty ");
        else {
            while (current != null) {
                System.out.println(current.value);
                current = current.next;
            }
        }
    }
}

and its showing error messages that read

List.java:19: cannot resolve symbol
symbol : method printStackTrace()
location: class List
            printStackTrace();

List.java:20: cannot resolve symbol
symbol : method printLn (java.lang.String)
location : class java.io.PrintStream
        System.out.printLn("Invalid");

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