Fix Java Linked List Output Issue & Error

  • Java
  • Thread starter XodoX
  • Start date
  • Tags
    Java List
In summary, the conversation discusses an issue with no output when compiling a program. The program is meant to create a linked list of cars, sort them, and display the sorted results. The issue could potentially be related to the incorrect package declaration at the top of every file. Additionally, there is a reported error in the LinkedList.java file, specifically with a string concatenation. It is suggested to use Netbeans' refactoring tool to move the files into a package.
  • #1
XodoX
203
0
Can someone tell me why there's no output? I compiled it, and it should work, but there's simply no output (netbeans). It also says "incorrect package" at the top of every file.
It's a linked list that adds cars, sorts them, and (should) display the sorted results.
There's also an error in LinkedList.java. At the bottom - "str.append(cursor+"\n");" it says String concentration in string buffer?


Driver.java
Code:
public class Driver
{

    public static void main(String[] arg)
    {
    
        LinkedList list = new LinkedList();
        
        list.addCar("Porsche 911",312,1994);
        list.addCar("Porsche 911",284,1992);
        list.addCar("BMW M3 ",286,1993);
        list.addCar("Volvo 850",170,1992);
        list.addCar("VW VR6",170,1997);
        
        //Method sort the list after year the car was made
        list.sortList();
        
        //Method to print all objects in List
        System.out.println(list.viewAll());
            
    }
}

LinkedList.java

Code:
import java.util.*;
public class LinkedList
{
    
    private CarNode head = null;
    
    public void addCar(String name , int hk , int year)
    {    
        //If head = null then create the first node
        if(head == null)
        {
            head = new CarNode(name,hk,year,null);
        }
        else
        {
            //If there are more than 1 node
            head = new CarNode(name,hk,year,head);            
        }
                
    }


    public void sortList()
    {
        
        boolean sorted = false;
        
        while(!sorted)
        {
            
            sorted = true;
            
            for(CarNode cursor = head ; cursor.getNext() != null ; cursor = cursor.getNext())
            {
                if(cursor.getYear() < cursor.getNext().getYear())
                {
                    String n = cursor.getName();
                    int y = cursor.getYear();
                    int hk = cursor.getHk();
                    
                    cursor.setName(cursor.getNext().getName());
                    cursor.setYear(cursor.getNext().getYear());
                    cursor.setHk(cursor.getNext().getHk());
                    
                    cursor.getNext().setName(n);
                    cursor.getNext().setYear(y);
                    cursor.getNext().setHk(hk);
                                                        
                    sorted = false;        
                }
                    
            
            }
                    
            
        }
        
        
        
        
    }
    

    public String viewAll()
    {
            
        StringBuffer str = new StringBuffer();    
                
        for(CarNode cursor = head ; cursor != null ; cursor = cursor.getNext())
        {
            //Appending car by car until there are no more cars
            str.append(cursor+"\n");
        }
        return new String(str);
        
    }

    
}

CarNode.java

Code:
public class CarNode
{
    private String name;
    private int hk;
    private int year;
    private CarNode next;
    
    public CarNode(String name,int hk,int year,CarNode next)
    {
        this.name = name;
        this.hk = hk;
        this.year = year;
        this.next = next;
            
    }



    public CarNode getNext()
    {
        return next;
    }
    
    public String getName()
    {
        return name;
    }
    
    public int getHk()
    {
        return hk;
    }
    
    public int getYear()
    {
        return year;
    }
    
    
    public void setName(String in)
    {
        name = in;
    }
    
    public void setHk(int in)
    {
        hk = in;
    }
    
    public void setYear(int in)
    {
        year = in;
    }
    
    
    public String toString()
    {
        return name + " " + hk + " " + year;
    }
    
    
}
 
Technology news on Phys.org
  • #2
You almost surely want to move your files into a package [1] by including a package declaration on top of each source file and move the source file to the folder corresponding to this package. Netbeans do, as far as I know, offer a refactoring tool for this.[1] http://docs.oracle.com/javase/tutorial/java/package/packages.html
 

1. How do I fix an output issue in a Java linked list?

The most common cause of output issues in a Java linked list is a mistake in the code. Double-check your code for any syntax errors or logical mistakes that may be causing the incorrect output.

2. Why am I getting an error when working with a Java linked list?

There are many different types of errors that can occur when working with a Java linked list. Some common causes of errors include incorrect data types, null values, and out-of-bounds indexes. Carefully review your code and debug any potential issues.

3. How can I ensure that my Java linked list is functioning properly?

To ensure that your Java linked list is functioning properly, you can write unit tests to check for expected outputs and edge cases. This will allow you to catch any errors or issues before they become a problem in your code.

4. What is the best way to iterate through a Java linked list?

The most efficient way to iterate through a Java linked list is by using a for loop with the size of the list as the condition, and accessing each element using the get() method. Alternatively, you can also use a while loop with a current node variable that moves to the next node with each iteration.

5. How can I fix a null pointer exception in my Java linked list?

A null pointer exception in a Java linked list usually means that you are trying to access or manipulate a null value. Check your code for any null values and make sure that they are properly handled to avoid this error.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
839
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top