请帮助调试此程序中的错误

时间:2011-04-06 14:42:02

标签: java debugging linked-list compiler-errors

我不确定是不是因为我没有睡觉,或者因为我以前从未遇到过,但我不知道这个错误意味着什么。我正在尝试编译一个使用自定义对象与双向链表进行交互的程序,编译器一直告诉我以下内容:

    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

这是我的代码:

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;
  }

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

错误在行

  public Product search (String key)

原因是java只接受顶层的类,接口或枚举定义(因为消息非常明确地说明了这一点)。

答案 1 :(得分:1)

方法:

public Product search (String key)

是在课堂外定义的。

相关问题