请帮助调试该程序中的错误
我不确定是因为我没有睡觉还是因为我以前从未遇到过它,但我不知道这个错误意味着什么。我正在尝试编译一个使用自定义对象与双向链表交互的程序,编译器不断告诉我以下内容:
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;
}
非常感谢任何帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误出现在该行
,原因是 java 仅接受顶层的类、接口或枚举定义(正如消息中明确指出的那样)。
The error is at line
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:
is defined outside your class.