我正在为一个类编写一个地址簿程序,在我的代码中的某个地方,我已经完全迷失了,似乎无法找出为什么我会遇到特定的错误。它目前由3个程序组成。 Menu.java,JGAddressBook.java和JursekGregChapter10t.java
import java.util.ArrayList;
import java.util.Scanner;
public class JursekGregChapter10t
{
public static void main(String [] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Welcome to JGAddressBook");
ArrayList<JGAddressBook> list = new ArrayList<JGAddressBook>();
while(true)
{
display();
int ch=s.nextInt();
if(ch==0)
{
System.out.println("Please Enter First Name: ");
String a=s.nextLine();
System.out.println("Please Enter Last Name: ");
String b=s.nextLine();
System.out.println("Please Enter Street Address: ");
String c=s.nextLine();
System.out.println("Please Enter City, State: ");
String d=s.nextLine();
System.out.println("Please Enter Zip Code: ");
String e=s.nextLine();
JGAddressBook item =new JGAddressBook(a,b,c,d,e);
list.add(item);
}
else if((ch==1)||(ch==2)||(ch==3)||(ch==4)||(ch==5))
{
System.out.println("Please enter value to search for: ");
String q=s.nextLine();
JGAddressBook temp=search(list,q,ch);
if(temp==null)
{
System.out.println("No Entry Found");
}
else
{
System.out.println("First Name: "+temp.FirstName);
System.out.println("Last Name: "+temp.LastName);
System.out.println("Street Address: "+temp.StreetAddress);
System.out.println("City,State: "+temp.CityState);
System.out.println("Zip Code: "+temp.ZipCode);
System.out.println();
}
}
else if(ch==6)
{
break;
}
else
{
System.out.println("Error: Invalid input");
}
}
}
}
这是第一个程序,第二个是第二个程序,然后是第三个
import java.io.*;
import java.util.*;
class JGAddressBook {
String FirstName;
String LastName;
String StreetAddress;
String CityState;
String ZipCode;
public JGAddressBook()
{
FirstName="";
LastName="";
StreetAddress="";
CityState="";
ZipCode="";
}
public JGAddressBook(String a,String b,String c,String d,String e)
{
FirstName=a;
LastName=b;
StreetAddress=c;
CityState=d;
ZipCode=e;
}
public void addEntry(String a,String b,String c,String d,String e)
{
FirstName=a;
LastName=b;
StreetAddress=c;
CityState=d;
ZipCode=e;
}
}
和第三个......
import java.util.ArrayList;
public class Menu
{
public static void display()
{
System.out.println("Menu: Press the following");
System.out.println("0. Add New Entry");
System.out.println("1. Search First Name");
System.out.println("2. Search Last Name");
System.out.println("3. Search Street Address");
System.out.println("4. Search City, State");
System.out.println("5. Search Zip Code");
System.out.println("6. Exit ");
}
public static JGAddressBook search(ArrayList A, String c, int field)
{
JGAddressBook item=new JGAddressBook();
for (int i=0; i < A.size(); i++)
{
item=(JGAddressBook) A.get(i);
if(field==1)
{
if(item.FirstName.equals(c))
return item;
}
else if(field==2)
{
if(item.LastName.equals(c))
return item;
}
else if(field==3)
{
if(item.StreetAddress.equals(c))
return item;
}
else if(field==4)
{
if(item.CityState.equals(c))
return item;
}
else
{
if(item.ZipCode.equals(c))
return item;
}
}
return null;
}
}
我得到的错误是在编译第一个程序时,如下所示:
/Users/Greg/Documents/Programming/Java/JursekGregChapter10t.java:15: error: cannot find symbol
display();
^
symbol: method display()
location: class JursekGregChapter10t
/Users/Greg/Documents/Programming/Java/JursekGregChapter10t.java:43: error: cannot find symbol
JGAddressBook temp=search(list,q,ch);
^
symbol: method search(ArrayList<JGAddressBook>,String,int)
location: class JursekGregChapter10t
2 errors
[Finished in 0.6s with exit code 1]
答案 0 :(得分:1)
首先出现错误,您需要在static
对象上调用display()
Menu
方法,而不是尝试从display()
类调用JursekGregChapter10t
:< / p>
Menu.display();
其次,您还在调用search(list,q,ch)
,因为您应该在Menu对象上调用search(...)
:
Menu.search( list, q, ch );
编辑以寻找进一步的问题 - 当您使用nextInt()
方法时,它不会消耗该行的其余部分。这意味着当您在界面中提示您输入值时,nextInt()
方法会收集该值。然而,当您使用nextLine()
方法作为名字时,nextLine()
会消耗其余部分,并包括\n
。这意味着它会根据您的输入消耗所有内容(就在它之前)。因此,当为姓氏调用nextLine()
时,它会使用您的名字和姓氏(最多为\n
)。
int ch=s.nextInt();
这是修复:
int ch=s.nextInt();
s.nextLine();