Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4252808
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:51:57+00:00 2026-05-21T04:51:57+00:00

I’m not sure if it’s because I haven’t slept or because I’ve never encountered

  • 0

I’m not sure if it’s because I haven’t slept or because I’ve never encountered it before, but I have no idea what this error means. I’m trying to compile a program that interacts with a doubly linked list using a custom object, and the compiler keeps telling me the following:

    19 errors found:
File: Z:\CS121\Project 6\prog6.java  [line: 127]
Error: Z:\CS121\Project 6\prog6.java:127: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 130]
Error: Z:\CS121\Project 6\prog6.java:130: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 134]
Error: Z:\CS121\Project 6\prog6.java:134: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 136]
Error: Z:\CS121\Project 6\prog6.java:136: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 138]
Error: Z:\CS121\Project 6\prog6.java:138: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 140]
Error: Z:\CS121\Project 6\prog6.java:140: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 143]
Error: Z:\CS121\Project 6\prog6.java:143: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 144]
Error: Z:\CS121\Project 6\prog6.java:144: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 145]
Error: Z:\CS121\Project 6\prog6.java:145: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 146]
Error: Z:\CS121\Project 6\prog6.java:146: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 149]
Error: Z:\CS121\Project 6\prog6.java:149: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 151]
Error: Z:\CS121\Project 6\prog6.java:151: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 156]
Error: Z:\CS121\Project 6\prog6.java:156: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 158]
Error: Z:\CS121\Project 6\prog6.java:158: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 160]
Error: Z:\CS121\Project 6\prog6.java:160: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 161]
Error: Z:\CS121\Project 6\prog6.java:161: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 165]
Error: Z:\CS121\Project 6\prog6.java:165: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 166]
Error: Z:\CS121\Project 6\prog6.java:166: class, interface, or enum expected
File: Z:\CS121\Project 6\prog6.java  [line: 169]
Error: Z:\CS121\Project 6\prog6.java:169: class, interface, or enum expected

This is my code:

import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;


class prog6 {
  public static void main(String args[]) throws FileNotFoundException
  {
    Scanner kb=new Scanner(System.in);
    String tempName, keyName;
    Product tempProduct, temp;
    double tempPrice;
    boolean found;

    /* Here, add code to delcare and/or create a linked list
     and, if necessary, an iterator.
     */
    LinkedList<Product> list = new LinkedList<Product>();
    ListIterator<Product> iter = list.listIterator();

    //the following code reads product data from the file data.txt
    //and saves the product data in the linked list

    Scanner infile=new Scanner(new File("data.txt"));

    while(infile.hasNextLine())
    {
      tempProduct=tempProduct.addFirst(infile.nextLine(), infile.nextDouble());
      infile.nextLine(); //skip end-of-line

      /*here, add code to add the object tempProduct 
       to the beginning of the linked list*/

    }
    infile.close();

    while(true)
    {
      System.out.println();
      System.out.println();

      //show prompt messages and menu
      System.out.println("Please select one of the follwing actions:");
      System.out.println("q - Quit");
      System.out.println("a - print the product list");
      System.out.println("b - add a product");
      System.out.println("c - enter a product name to find the product record");
      System.out.println("d - delete a product");
      System.out.println("Please enter q, a, b, c, or d:");

      String selection=kb.nextLine();  //read user's selection
      if (selection.equals("")) continue; //if selection is "", show menu again

      switch (selection.charAt(0))
      {
        case 'q':
          System.out.println("Thank you");
          return;

          /*write code for the cases 'a','b','c' and 'd' */

        case 'a':
          temp = list;
          while (temp != null) 
          { 
            System.out.println (temp.data);
            temp = temp.next;
          }
          System.out.println();
          break;

        case 'b':
          System.out.println("Please enter the name of the new product:");
          tempName = kb.nextLine();
          System.out.println("Please enter the price of the new product:");
          tempPrice = kb.nextDouble();

          temp = new node(tempName, tempPrice);
          temp.next = list;
          list = temp;
          break;

        case 'c':
          System.out.println("Please enter a product name:");
          n=kb.nextLine();
          tempProduct = list.search(n);

          if(tempProduct == null)
            System.out.println("Cannot find product named " + n);
          else
            System.out.println("Product found: " + tempProduct);
          break;

        case 'd':
          System.out.println("Please enter name of a customer to remove from list:");
          tempString = kb.nextLine();
          list = delete(list, tempString);
          break;

        default: System.out.println("Incorrect selection.\n");
      } //end switch
    } //end while
  } //end main();

} //end class prog6


class Product
{
  String name;
  double price;

  public Product(String n, double p)
  { name=n;
    price=p;
  } //end Constructor

  public String toString()
  {
    return String.format("%-20s%10.2f", name, price);
  }

} //end class Product

  public Product search (String key)
  {
    node temp = head;
    while (temp != null)
    {
      if (temp.data.name.equals(key))
        return temp.data;
      else
        temp = temp.next;
    }
    return null;
  }

  public void add (Product a)
  {
    node temp = new node (a);
    temp.next = head;
    head = temp;
  }
  public Boolean delete (String namekey, Product a)
  {
    node previous = null;
    node current = head;

    while (current != null)
    {
      if (current.name.equals(namekey))
      {
        current = current.next;
        if (previous == null)
          previous = current;
        else 
          previous.next = current;
        return true;
      }//end if
      else 
      {
        previous = current;
        current = current.next;
      }//end else
    }//end while
    return false;
  }

Any help is greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:51:57+00:00Added an answer on May 21, 2026 at 4:51 am

    The error is at line

      public Product search (String key)
    

    and the cause is that java accepts only class, interface or enum definitions at the top level (as the message quite clearly states).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.