gpt4 book ai didi

java - 地址簿 - 错误

转载 作者:行者123 更新时间:2023-12-01 13:12:31 26 4
gpt4 key购买 nike

我正在为一个类(class)开发一个地址簿程序,在我的代码中的某个地方我完全迷失了,似乎无法找出为什么我会遇到特定的错误。目前它由 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]

最佳答案

第一个错误,您需要在 Menu 对象上调用 static display() 方法,而不是尝试调用 display () 来自您的 JursekGregChapter10t 类:

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();

关于java - 地址簿 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22744319/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com