• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

getting errors in this code but when I had it in another code it worked

April 11, 2020 by

Questions › getting errors in this code but when I had it in another code it worked
0
Vote Up
Vote Down
Garmaine asked 3 years ago

The first 3 sets of code are from the same task which gives me errors. I have been trying for a couple weeks to find a solution to inputing strings into a linked list and i finally found a solution, so what I have tried to do is combine the solution i found along with the code i had written up prior to me researching and it still doesnt seem to work. any help would be greatly appreciated

package step2;
import java.util.Scanner;



public class Step2 {
/**
 * @param args the command line arguments
 */
public static void main(String[] args)
    {                 
        Scanner scan = new Scanner(System.in);
        /* Creating object of BST */
        System.out.println("Binary Search Tree Test\n");          
        char ch;
        /*  Perform tree operations  */
        do    
        {
            System.out.println("\nBinary Search Tree Operations\n");
            System.out.println("1. insert ");
            System.out.println("2. delete");
            System.out.println("3. search");
            System.out.println("4. count nodes");
            System.out.println("5. check empty"); 
            System.out.println("6. display Binary Tree");

            System.out.print("Please enter option: ");
            int choice = scan.nextInt();            
            switch (choice)
            {
            case 1 : 
                do{
                    LinkedList<Employee> linkedList = new LinkedList<Employee>(); // creation of Linked List
                    System.out.print("Enter employee name: ");
                    String name = scan.nextLine();
                    System.out.print("Enter employee position: ");
                    String position = scan.nextLine();
                    linkedList.insertFirst(new Employee( name, position));

                    linkedList.displayLinkedList(); // display LinkedList

                                             //display deleted Node.


                    System.out.println("\nDo you want to continue (Type y or n) \n");
                    ch = scan.next().charAt(0);                        
                } while (ch == 'Y'|| ch == 'y');   
                break;                          
            case 2 : 
                    System.out.print("Deleted Nodes: ");
                    Node<Employee> deletedNode = linkedList.deleteFirst(); //delete Node
                    deletedNode.displayNode();                                 //display deleted Node.
                    deletedNode = linkedList.deleteFirst();      //delete Node.
                    deletedNode.displayNode();  
                break;                         
            case 3 : 

                break;                                          
            case 4 : 

                break;     
            case 5 :  

                break;   
            case 6 :

                break; 
            default : 
                System.out.println("Wrong Entry \n ");
                break;   
            }

            System.out.println("\nDo you want to continue (Type y or n) \n");
            ch = scan.next().charAt(0);                        
        } while (ch == 'Y'|| ch == 'y');               
    }
}

and this

package step2;

class Employee {
private String name;
private String position;

public Employee(String name, String position) { 
       this.name = name;
       this.position = position;
}

@Override
public String toString() {
       return "Employee [name=" + name + ", position=" + position + "]   ";
}

}

class LinkedListEmptyException extends RuntimeException{
   public LinkedListEmptyException(){
     super();
   }

 public LinkedListEmptyException(String message){
     super(message);
   }  
}

class Node<T> {
public T data; 
public Node<T> next; 

public Node(T data){
       this.data = data;
}

public void displayNode() {
       System.out.print( data + " ");
}
}

class LinkedList<T> {
private Node<T> first; 

public LinkedList(){
       first = null;
}

public void insertFirst(T data) {
       Node<T> newNode = new Node<T>(data);  
       newNode.next = first;   
       first = newNode;  
}

public Node<T> deleteFirst()
{
       if(first==null){  
              throw new LinkedListEmptyException("LinkedList doesn't contain any Nodes.");
       }
       Node<T> tempNode = first; 
       first = first.next; 
       return tempNode; 
}

public void displayLinkedList() {
       System.out.print("Displaying LinkedList: ");
       Node<T> tempDisplay = first; 
       while (tempDisplay != null){ 
              tempDisplay.displayNode();
              tempDisplay = tempDisplay.next; 
       }
       System.out.println();

}

}

and this is part 3 of my code

package step2;

class linkedList {

static Node<Employee> deleteFirst() {
    throw new UnsupportedOperationException("Not supported yet."); 
}

}

this is the code i took from

package singlylinkedlistgenericexample;

import java.util.Scanner;

class Employee {
private String name;
private String position;

public Employee(String name, String position) { // constructor
       this.name = name;
       this.position = position;
}

@Override
public String toString() {
       return "Employee [name=" + name + ", position=" + position + "]   ";
}

}

class LinkedListEmptyException extends RuntimeException{
   public LinkedListEmptyException(){
     super();
   }

 public LinkedListEmptyException(String message){
     super(message);
   }  
}

class Node<T> {
public T data; 
public Node<T> next; 

public Node(T data){
       this.data = data;
}

public void displayNode() {
       System.out.print( data + " ");
}
}

class LinkedList<T> {
private Node<T> first; 

public LinkedList(){
       first = null;
}

public void insertFirst(T data) {
       Node<T> newNode = new Node<T>(data);  
       newNode.next = first;   
       first = newNode;  
}

public Node<T> deleteFirst()
{
       if(first==null){  //means LinkedList in empty, throw exception.              
              throw new LinkedListEmptyException("LinkedList doesn't contain any Nodes.");
       }
       Node<T> tempNode = first; 
       first = first.next; 
       return tempNode; 
}

public void displayLinkedList() {
       System.out.print("Displaying LinkedList [first--->last]: ");
       Node<T> tempDisplay = first; 
       while (tempDisplay != null){ 
              tempDisplay.displayNode();
              tempDisplay = tempDisplay.next; 
       }
       System.out.println();

}
}

public class SinglyLinkedListGenericExample {
public static void main(String[] args) {
    char ch;
    do{
        LinkedList<Employee> linkedList = new LinkedList<Employee>(); // creation of Linked List
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter employee name: ");
        String name = scan.nextLine();
        System.out.print("Enter employee position: ");
        String position = scan.nextLine();
        linkedList.insertFirst(new Employee( name, position));

        linkedList.displayLinkedList(); // display LinkedList

        System.out.println("\nDo you want to continue (Type y or n) \n");
        ch = scan.next().charAt(0);                        
    } while (ch == 'Y'|| ch == 'y');   
}
}

and this is the error message i am getting:

ant -f C:\\Users\\McClu\\Desktop\\desktop\\JaveMainCoursework\\step2\\step2 - 
Dnb.internal.action.name=run run
init:
Deleting: 
C:\Users\McClu\Desktop\desktop\JaveMainCoursework\step2\step2\build\built- 
jar.properties
deps-jar:
Updating property file: 
C:\Users\McClu\Desktop\desktop\JaveMainCoursework\step2\step2\build\built- 
jar.properties
compile:
run: 
Binary Search Tree Test


Binary Search Tree Operations

1. insert 
2. delete
3. search
4. count nodes
5. check empty
6. display Binary Tree
Please enter option: 1
Exception in thread "main" java.lang.NoClassDefFoundError: step2/linkedList (wrong name: step2/LinkedList)
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:821)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:719)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:642)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:600)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at step2.Step2.main(Step2.java:41)
C:\Users\McClu\Desktop\desktop\JaveMainCoursework\step2\step2\nbproject\build-impl.xml:1330: The following error occurred while executing this line:
C:\Users\McClu\Desktop\desktop\JaveMainCoursework\step2\step2\nbproject\build-impl.xml:936: Java returned: 1
BUILD FAILED (total time: 3 seconds)
Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: java

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements architecture beautiful life best building calling city commercial convenience employment Finances Cognitive decline Future gadgets Hidden Gems highway Home houses hydration Impact Innovations lamp lighting Mental health military tech Must-See New York City occupation Productivity recreation romance sepia shopping sippy cups smartphones social Technological breakthroughs technology toddlers Treasures turns Uncover Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements architecture beautiful life best building calling city commercial convenience employment Finances Cognitive decline Future gadgets Hidden Gems highway Home houses hydration Impact Innovations lamp lighting Mental health military tech Must-See New York City occupation Productivity recreation romance sepia shopping sippy cups smartphones social Technological breakthroughs technology toddlers Treasures turns Uncover Well-being Wonders Work Young onset dementia

Copyright © 2023