hi, I'm working on an exercise, i did a lot of work already and i just can't figure where I'm going wrong, this is what I'm trying to achieve

Sample IO

*******************************************************************************
Welcome to PeopleSoft 2
MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it
a
Enter the student number
MSXMIC001
Enter the name
Mickey Mouse
Done
MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it
a
Enter the student number
CHPALV001
Enter the name
Alvin the Chipmunk
Done
MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it
a
Enter the student number
DCKDON002
Enter the name
Donald Duck
Done
MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it
l
Student: DCKDON002, Donald Duck
Student: CHPALV001, Alvin the Chipmunk
Student: MSXMIC001, Mickey Mouse
Done
MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it
s
Done
MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it
l
Student: CHPALV001, Alvin the Chipmunk
Student: DCKDON002, Donald Duck
Student: MSXMIC001, Mickey Mouse
Done
MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it
d
Enter the student number
DCKDON002
Done
MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it
l
Student: CHPALV001, Alvin the Chipmunk
Student: MSXMIC001, Mickey Mouse
Done
MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it
x
***************************************************************

and this is my code so far, that won't work....

*******************************************************************************************
import java.util.NoSuchElementException;
import java.util.Scanner;

public class LinkedList2
{   private class Node
    {
            private String name;
            private String stuName;
            private Node link;


    public Node()
    {
        link = null;
        name = null;
        stuName = null;
    }

    public Node(String newStuName, String newName, Node linkValue)
    {
        setData(newStuName, newName);
        link = linkValue;
    }
    public void setData(String newStuName, String newName)
    {
        name = newName;
        stuName = newStuName;
    }
    public void setLink(Node newLink)
    {
        link = newLink;
    }
    public String getStuName()
    {
        return stuName;
    }
    public String getName()
    {
        return name;
    }
    public Node getLink()
    {
        return link;
    }
    }//end of node inner class

    public class List2Iterator//inner class for iterators for linkedlist2
    {   
        private Node position;
        private Node previous;

        public List2Iterator()
        {
            position = head;
            previous = null;
        }
        public void restart()
        {
            position = head;
            previous = null;
        }
        public String next()
        {
            if(!hasNext())
                throw new NoSuchElementException();
            String toReturn = position.stuName;
            previous = position;
            position = position.link;
            return toReturn;
        }
        public boolean hasNext()
        {
            return(position != null);
        }
        public String peek()
        {
            if(!hasNext())
                throw new IllegalStateException();
                return position.stuName;
        }
        public void addHare(String newData)
        {
            if(position == null && previous != null)//if at end o list

            previous.link = new Node(newData, null);
            else if(position == null || previous == null)
            LinkedList2.this.addToStart(newData);

            else{
                Node temp = new Node(newData, position);
                previous.link = temp;
                previous = temp;
            }
        }
        public void changeHare(String newData)
        {
            if(position == null)
                throw new IllegalStateException();
                else
                position.stuName = newData;
        }
        public void delete()
        {
            if(position == null)
                throw new IllegalStateException();
                else if(previous == null)
                {
                    head = head.link;
                    position = head;
                }
                else
                {
                    previous.link = position.link;
                    position = position.link;
                }
        }
    }
        private Node head;

        public List2Iterator iterator()
        {
            return new List2Iterator();
        }
        public LinkedList2()
        {
            head = null;//adds node at the stat of the list with the specified data
        }
        public void addToStart(String itemStuName, String itemName)
        {
        head = new Node1(itemStuName, itemName, head);
        }
        }
        public boolean deleteHeadNode()
        {
            if(head != null){

                head = head.link;
                return true;
            }
            else 
            return false;
        }
        /*public int size()//return number of nodes in the list
        {
            int count = 0;
            Node position = head;
            while(position != null)
            {
                count++;
                position = position.link;
            }
            return count;
        }*/
        public boolean contains(String stuName, String name)
        {
        return (find(stuName, name) != null); 
        }
        private Node find(String target)
        {
            Node position = head;
            String itemAtPosition;
            while (position != null)
            {
                itemAtPosition = position.stuName;
                if(itemAtPosition.equals(target))
                    return position;
                    position = position.link;
            }
            return null; //target wan not found
        }
        public void outPutList()
        {
            Node position = head;
            while(position != null);
            {
                System.out.println(position.stuName);
                position = position.link;
            }
        }
        public boolean isEmpty()
        {
            return(head == null);
        }
        public void clear()
        {
            head = null;
        }
        public boolean equals(Object otherObject)
        {
            if(otherObject == null)
                return false;
                else if(getClass() != otherObject.getClass())
                    return false;
                else{
                    LinkedList2 otherList = (LinkedList2)otherObject;
                    if(size() != otherList.size())
                        return false;
                        Node position = head;
                        Node otherPosition = otherList.head;
                        while(position != null)
                        {   
                            if((!(position.item.equals(otherPosition.stuName))))
                                return false;
                            position = position.link;
                            otherPosition = otherPosition.link;
                        }
                        return true;//a mis match was not found
                    }
        }


        public static void main(String[] args)
        {
            Scanner keyboard = new Scanner(System.in);

            LinkedList2 list = new LinkedList2();
            LinkedList2.List2Iterator i = list.iterator();

            System.out.println("Welcome to PeopleSoft 2");

            int choice = keyboard.nextInt();
            while(choice !== 5)
            {
                System.out.println("MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it");            
                switch(choice)
                {
                    case 1:
                    System.out.println("Enter the student number");
                    stuNum = keyboard.next();
                    System.out.println("Enter the Student name");
                    name = keyboard.next();
                    list.addToStart(studentNum,name);
                    System.out.println("Done");
                    break;

                    case 2:
                    System.out.println("Enter student number");
                    stuName = keyboard.next();
                    i.restart();
                    i.next();
                    System.out.println("Will delete the node for; " + i.peek());
                    i.delete();
                    System.out.println("Done");
                    break;

                    case 3:
                    System.out.println("list now contains:");
                    i.restart();
                    while(i.hasNext())
                    System.out.println("Student: " + i.next());
                    System.out.println();
                    System.out.println("Done");
                    break;

                    case 5:
                    System.exit(0);
                    break;

                    default:
                    System.out.println("Error,please check your input again");
                    break;  
                }
            }       



        }


}
********************************************************************************************

please help me see where I'm going wrong.

Dani AI

Generated

Quick diagnosis for : the bulk of the compiler messages (the common "class, interface, or enum expected" / "enum expected" family of errors) point to top-level syntax problems — most often mismatched braces or declarations that ended up outside the class. Other clear issues in the pasted text are Node1 used where only Node is declared, a stray semicolon after while(position != null);, the Java operator != written as !==, and several undeclared or mismatched variable/method names (stuNum / studentNum / name, contains vs find, position.item when no item exists). Those will produce cascades of errors.

Concrete, ordered fixes (apply and recompile after each step):

  • Reformat the file in an IDE or editor that shows matching braces; fix any misplaced } so methods remain inside the class.
  • Replace Node1 with Node (or add the Node1 class if intended).
  • Remove stray semicolons after control statements (while(...) ;), and correct !==!=.
  • Declare local variables before use (String stuNum, String name, etc.).
  • Make method signatures consistent: have contains(String id) call find(String id). Fix equals to use the real node fields.
  • Compile frequently; fix the first syntax error reported — many later messages are follow-on faults.

Two small, safe examples to apply immediately (these are templates; adapt field names to the Node class already present):

public boolean deleteById(String id) {
    if (head == null) return false;
    if (head.stuName.equals(id)) { head = head.link; return true; }
    Node prev = head;
    Node cur = head.link;
    while (cur != null) {
        if (cur.stuName.equals(id)) { prev.link = cur.link; return true; }
        prev = cur;
        cur = cur.link;
    }
    return false;
}
Scanner in = new Scanner(System.in);
LinkedList2 list = new LinkedList2();
while (true) {
    System.out.println("MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it");
    String line = in.nextLine().trim();
    if (line.isEmpty()) continue;
    char cmd = Character.toUpperCase(line.charAt(0));
    if (cmd == 'X') break;
    switch (cmd) {
        case 'A': /* read id/name with nextLine() then list.addToStart(id, name); */ break;
        case 'L': list.printList(); break;
        case 'D': list.deleteById(in.nextLine().trim()); break;
        default: System.out.println("Error, please check input");
    }
}

is right to push reading the compiler output — fix the first error and most others will disappear. 's advice to format the code will make brace and semicolon problems obvious. Using an IDE (Eclipse/IntelliJ) or a good text editor and compiling after small edits will speed diagnosis dramatically.

Recommended Answers

All 4 Replies

tons of unformatted code, no description of what "goes wrong", no nothing.
You'll have to do better than that if you expect us to fix the code you pulled from some other website for you, kid.

could you put the code between [ CODE] [ /CODE] tags to make it more readable and also give the errors you are getting? might make it a lot easier to actually help you out with this one

hi again, sorry I did not give enough information,
when i compile, I get about 70 lines of errors saying "enum expected" what i really have trouble with is letting the user enter all the information. I'm also having trouble writing a suitable delete() method....the one in my code is from my textbook example, actualy almost of all the code...all I really need is how do let the user enter all the data and do I need an array for (studentNumber and name)?..I'm a bit frustrated now.
[import java.util.NoSuchElementException;
import java.util.Scanner;

public class LinkedList2
{ private class Node
{
private String name;
private String stuName;
private Node link;


public Node()
{
link = null;
name = null;
stuName = null;
}

public Node(String newStuName, String newName, Node linkValue)
{
setData(newStuName, newName);
link = linkValue;
}
public void setData(String newStuName, String newName)
{
name = newName;
stuName = newStuName;
}
public void setLink(Node newLink)
{
link = newLink;
}
public String getStuName()
{
return stuName;
}
public String getName()
{
return name;
}
public Node getLink()
{
return link;
}
}//end of node inner class

public class List2Iterator//inner class for iterators for linkedlist2
{
private Node position;
private Node previous;

public List2Iterator()
{
position = head;
previous = null;
}
public void restart()
{
position = head;
previous = null;
}
public String next()
{
if(!hasNext())
throw new NoSuchElementException();
String toReturn = position.stuName;
previous = position;
position = position.link;
return toReturn;
}
public boolean hasNext()
{
return(position != null);
}
public String peek()
{
if(!hasNext())
throw new IllegalStateException();
return position.stuName;
}
public void addHare(String newData)
{
if(position == null && previous != null)//if at end o list

previous.link = new Node(newData, null);
else if(position == null || previous == null)
LinkedList2.this.addToStart(newData);

else{
Node temp = new Node(newData, position);
previous.link = temp;
previous = temp;
}
}
public void changeHare(String newData)
{
if(position == null)
throw new IllegalStateException();
else
position.stuName = newData;
}
public void delete()
{
if(position == null)
throw new IllegalStateException();
else if(previous == null)
{
head = head.link;
position = head;
}
else
{
previous.link = position.link;
position = position.link;
}
}
}
private Node head;

public List2Iterator iterator()
{
return new List2Iterator();
}
public LinkedList2()
{
head = null;//adds node at the stat of the list with the specified data
}
public void addToStart(String itemStuName, String itemName)
{
head = new Node1(itemStuName, itemName, head);
}
}
public boolean deleteHeadNode()
{
if(head != null){

head = head.link;
return true;
}
else
return false;
}
/*public int size()//return number of nodes in the list
{
int count = 0;
Node position = head;
while(position != null)
{
count++;
position = position.link;
}
return count;
}*/
public boolean contains(String stuName, String name)
{
return (find(stuName, name) != null);
}
private Node find(String target)
{
Node position = head;
String itemAtPosition;
while (position != null)
{
itemAtPosition = position.stuName;
if(itemAtPosition.equals(target))
return position;
position = position.link;
}
return null; //target wan not found
}
public void outPutList()
{
Node position = head;
while(position != null);
{
System.out.println(position.stuName);
position = position.link;
}
}
public boolean isEmpty()
{
return(head == null);
}
public void clear()
{
head = null;
}
public boolean equals(Object otherObject)
{
if(otherObject == null)
return false;
else if(getClass() != otherObject.getClass())
return false;
else{
LinkedList2 otherList = (LinkedList2)otherObject;
if(size() != otherList.size())
return false;
Node position = head;
Node otherPosition = otherList.head;
while(position != null)
{
if((!(position.item.equals(otherPosition.stuName))))
return false;
position = position.link;
otherPosition = otherPosition.link;
}
return true;//a mis match was not found
}
}


public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);

LinkedList2 list = new LinkedList2();
LinkedList2.List2Iterator i = list.iterator();

System.out.println("Welcome to PeopleSoft 2");

int choice = keyboard.nextInt();
while(choice !== 5)
{
System.out.println("MENU: (A)dd student, (D)elete, (L)ist, (S)ort, e(X)it");
switch(choice)
{
case 1:
System.out.println("Enter the student number");
stuNum = keyboard.next();
System.out.println("Enter the Student name");
name = keyboard.next();
list.addToStart(studentNum,name);
System.out.println("Done");
break;

case 2:
System.out.println("Enter student number");
stuName = keyboard.next();
i.restart();
i.next();
System.out.println("Will delete the node for; " + i.peek());
i.delete();
System.out.println("Done");
break;

case 3:
System.out.println("list now contains:");
i.restart();
while(i.hasNext())
System.out.println("Student: " + i.next());
System.out.println();
System.out.println("Done");
break;

case 5:
System.exit(0);
break;

default:
System.out.println("Error,please check your input again");
break;
}
}
]

more unformatted code...
And those error messages are quite clear. Learn to interpret them.

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.