开发者

Help debugging an error in this program, please

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!


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).


The method:

public Product search (String key)

is defined outside your class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜