I am working creating a fully encapsulated, homogeneous singly linked data structure. The Listing class and SinglyLinkedList class that are part of the whole application compile fine, but the problem that I am having is performing different operations such as insert, fetch, update, and delete inside of a do-while loop statement. For some reason when I compile my MainSinglyLinkedClass, the compiler keeps telling that the insert, fetch, update, and delete methods can't be applied when I attach to objects. What I am doing wrong? Can someone please help? Here is my MainSinglyLinkedClass application above.

public class Listing{

     private String name;   // key field 

     private int ID; 

     private double GPA; 

         public Listing(String name, int ID, double GPA){

       this.name= name; 
       this.ID= ID; 
       this.GPA=GPA; 
   } 

   public String toString() { 

      return "Student Name: "  + " "  + name  + " "  +  "Student ID: "   + " "  + ID  + " "  + "Student GPA: " + " "  + GPA; 
    }

   public Listing deepCopy(){ 

    Listing clone= new Listing(name, ID, GPA); 
     return clone; 

   }

  public int compareTo(String targetKey) 
  { 
     return(name.compareTo(targetKey));

  }

}

public class SinglyLinkedList{
    private Node h; 
   public SinglyLinkedList()
   { 
       h = new Node(); // list header
       h.l = null; 
       h.next = null; // dummy node
    } 

   public boolean insert(Listing newListing)
   {
      Node n = new Node();
      if(n == null)
        return false;
      else
       { 
          n.next = h.next;     
          h.next = n;       
          n.l = newListing.deepCopy();
          return true; 
       } 
    } 

   public Listing fetch(String targetKey)
   { 

      Node p = h.next; 
      while(p != null && !(p.l.compareTo(targetKey) == 0))
      { 
         p = p.next;  
      } 
      if(p != null) 
        return p.l.deepCopy(); 
      else 
       return null; 
  }

  public boolean delete(String targetKey) 
  { 
     Node q = h; 
     Node p= h.next; 
     while(p != null && !(p.l.compareTo(targetKey)==0))
     { 
       q = p; 
       p = p.next;    
     }
     if(p != null)
     { 
        q.next = p.next; 
        return true;
     } 
     else 
      return false; 
  } 
  public boolean update(String targetKey, Listing newListing)
  { 
     if(delete(targetKey) == false) 
        return false;
     else if(insert(newListing) == false)
        return false;
       return true;
   }
  public void showAll()
  { 
    Node p = h.next; 
    while(p != null)
    { 
      System.out.println(p.l.toString());
      p = p.next; 
    } 
  } 
  public class Node
  { 
    private Listing l; 
    private Node next; 
    public Node()
    { 

    } 
  } 

} 

import java.io.*;

public class MainSinglyLinkedList{ 

   public static void main(String[] args){ 

     BufferedReader obj= new BufferedReader(new InputStreamReader(System.in)); 
     int ch, temp; 
     SinglyLinkedList sll= new SinglyLinkedList();

   do
   { 

       // a user can choose between different operations by pressing a number 

      System.out.println("*****************(Menu Operations:)******************"); 

      System.out.println("(Press 1). Insert a new student's information."); 
      System.out.println("(Press 2). Fetch and output a student's information"); 
      System.out.println("(Press 3). Delete a student's information."); 
      System.out.println("(Press 4). Update a student's information."); 
      System.out.println("(Press 5). Output all the student's information."); 
      System.out.println("(Press 6). Exit the program."); 
      System.out.println("Enter your choice."); 
      ch = Integer.parseInt(obj.readLine()); 
      switch(ch)
      { 
         case 1:System.out.println("Enter the Student's Name, ID, and GPA:  "); 
         sll.push(temp); 
         temp= Integer.parseInt(obj.readLine());

         break; 
         case 2: temp = sll.fetch(); 
         System.out.println("Here is the Student's Name, ID, and GPA you want to know about:    "); 
         break;
         case 3: temp = sll.delete();
          System.out.println("The student's information that has been deleted is:  ");
          break;
         case 4: temp = sll.update();
         System.out.println("The student's information that has been updated is: ");

        case 5: sll.showAll();
      }

  }while(ch!=6); 
} 

}

Recommended Answers

All 12 Replies

@rproffitt I am practicing out of this book. Do you think you could help me?

Well SMIca, I think the folk on the other discussion discussed what you need to do now. Start over and learn more. Jumping into the middle of the book would have you wondering why it doesn't work. Also, the solution to this one is on chegg.com if you want to buy an answer.

PS. Adding with edit. Crossposting is poor etiquette. When people find that, they may read your other discussion and agree with what is over there.

@rproffitt I don't want to buy the answer. I just didn't jump into the middle of the book either. I know that you have to add arguments to the method, but what I am confused about is the specific types of arguments that should be added those methods. I tried adding the scanner object with the readline() method inside each of the method and that didn't work. Any other suggestions besides telling me figure out on your own. It wasn't terribly bad to understand how to implement the methods when I did the stack and queue implementations. Any suggestions or resources of how I can solve my problem. I did try. Doesn't that count for anything anymore? It's not like I expected them to give me the answer; I honestly wanted to know how I could solve my problem.

I'd look at reducing the code to where you are having the issue and then sharing that. Surely the problem can be reduced to less than what you posted. From the other discussion, they explained it to you also. Here it is for you to read again.

"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Elliott Frisch, shmosel, khelwood, Vince Emigh, Robert Columbia

When you try to call those methods you are missing parameters or using parameters of the wrong type.

what I am confused about is the specific types of arguments that should be added those methods

Look at the definitions of those methods. The first line tells you the name of the method and the exact number and types of each paramter.
Eg

public boolean update(String targetKey, Listing newListing)

Requires 2 parameters, one String and one instance of Listing, in that order

@JamesCherrill Wouldn't I have let the user load some initial student's information before I even let the user see and select the list of choices?

That makes sense... only options 1 and 6 can be used at first because you can’t fetch, delete etc until there is something to fetch, delete etc.
Or maybe you could prime the system by creating and adding a few Listings in you main method right at the start?

@Daron_1. Since priming the data would be dummy data and not how you would in the real world, you would not offer those menu items until there is data in the database. You might add a Status bar with a record count and if zero, maybe say "No Records." The menu would grey out the unavailable items.

I agree that’s how it works in the real world. But this is a training exercise, and the menu is a console print with greying not an option.
Personally I have no problem with fake data to test or demo some list handling.

@JamesCherrilll.

And that's why its so important for training to cover this when they are young. Another example is the common use in SQL to use "SELECT *". I can't remember how many performance issues I've cleared up by correcting that one, but that's how they were taught so that's how they code.

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.