Java Fix Java Linked List Output Issue & Error

  • Thread starter Thread starter XodoX
  • Start date Start date
  • Tags Tags
    Java List
AI Thread Summary
The discussion centers on a Java program that is not producing output after compilation in NetBeans, with users noting an "incorrect package" error at the top of each file. The program is designed to manage a linked list of cars, allowing for addition, sorting, and displaying of car details. Key issues identified include a potential error in the `viewAll` method of `LinkedList.java`, where the line "str.append(cursor + "\n");" raises a concern about string concatenation in a string buffer. Additionally, participants suggest that the absence of output may be related to the lack of a package declaration in the source files, recommending the use of NetBeans' refactoring tools to properly organize the code into packages. This could resolve the compilation issues and enable the program to function as intended.
XodoX
Messages
195
Reaction score
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
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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top