为什么我不能使用我之前在 class 中创建的数组来打印出 phone 一本书的编号?
Why can't I use the array I made in a class earlier to print out the numbers for a phone book?
我正在尝试为学校项目制作 phone 一本书 reader。该程序要求输入名字和姓氏。姓名和 phone 人数存储在一个数组中。如果您不输入名字,而姓氏分配给多个人,程序应该会吐出多个人的 phone 号码。抱歉,可能有点长。
public static void main(String[] args) {
//Where the user input starts, gotta get dat number
Scanner scan = new Scanner(System.in);
System.out.print("First Name? ");
String first = scan.nextLine();
System.out.print("Last Name? ");
String last = scan.nextLine();
String fullname = first + last;
int[] count = new int[0];
int counter=0;
if(first.equals("quit") || last.equals("quit"))
System.out.println("Bye");
else
{
for(int l=0; l < 5; l++)
{
PhoneBook pb = new PhoneBook();
PhoneEntry entry = pb.search(fullname);
if( entry != null)
{
count[counter] = l; //this is where the issue lies
counter++;
}
}
}
if(count.length != 0)
{
for(int x=0; x<count.length; x++ )
{
PhoneBook pb = new PhoneBook();
System.out.println("Name: " + pb.phoneBook[count[x]].name + " Number: " + pb.phoneBook[count[x]].phone);
x++;
}
}
}
class PhoneEntry
{
String name; // name of a person
String phone; // their phone number
PhoneEntry( String n, String p )
{
name = n; phone = p;
}
}
class PhoneBook
{
PhoneEntry[] phoneBook;
PhoneBook() // constructor
{
phoneBook = new PhoneEntry[ 5 ] ;
phoneBook[0] = new PhoneEntry( "James Barclay", "(418)665-1223" );
phoneBook[1] = new PhoneEntry( "Grace Dunbar", "(860)399-3044" );
phoneBook[2] = new PhoneEntry( "Paul Kratides", "(815)439-9271" );
phoneBook[3] = new PhoneEntry( "Violet Smith", "(312)223-1937" );
phoneBook[4] = new PhoneEntry( "John Smith", "(913)883-2874" );
}
PhoneEntry search( String targetName )
{
for (int j=0; j<phoneBook.length; j++)
{
if ( phoneBook[ j ].
//stuff is to make it so that it only tests if its to upper case and if it contains the target
name.toUpperCase().contains( targetName.toUpperCase()))
return phoneBook[ j ];
}
return null;
}
}
非常感谢任何帮助。
您写道:
count[counter] = l; //this is where the issue lies
其实真正的问题在于:
int[] count = new int[0];
您已经创建了大小为零的数组;也就是说,你不能将任何元素放入到。如果您尝试这样做,您将得到一个例外。大概这就是正在发生的事情......
在Java中,数组的大小是固定的。您以给定的大小创建它们,并且它们将永远保持该大小。
我正在尝试为学校项目制作 phone 一本书 reader。该程序要求输入名字和姓氏。姓名和 phone 人数存储在一个数组中。如果您不输入名字,而姓氏分配给多个人,程序应该会吐出多个人的 phone 号码。抱歉,可能有点长。
public static void main(String[] args) {
//Where the user input starts, gotta get dat number
Scanner scan = new Scanner(System.in);
System.out.print("First Name? ");
String first = scan.nextLine();
System.out.print("Last Name? ");
String last = scan.nextLine();
String fullname = first + last;
int[] count = new int[0];
int counter=0;
if(first.equals("quit") || last.equals("quit"))
System.out.println("Bye");
else
{
for(int l=0; l < 5; l++)
{
PhoneBook pb = new PhoneBook();
PhoneEntry entry = pb.search(fullname);
if( entry != null)
{
count[counter] = l; //this is where the issue lies
counter++;
}
}
}
if(count.length != 0)
{
for(int x=0; x<count.length; x++ )
{
PhoneBook pb = new PhoneBook();
System.out.println("Name: " + pb.phoneBook[count[x]].name + " Number: " + pb.phoneBook[count[x]].phone);
x++;
}
}
}
class PhoneEntry
{
String name; // name of a person
String phone; // their phone number
PhoneEntry( String n, String p )
{
name = n; phone = p;
}
}
class PhoneBook
{
PhoneEntry[] phoneBook;
PhoneBook() // constructor
{
phoneBook = new PhoneEntry[ 5 ] ;
phoneBook[0] = new PhoneEntry( "James Barclay", "(418)665-1223" );
phoneBook[1] = new PhoneEntry( "Grace Dunbar", "(860)399-3044" );
phoneBook[2] = new PhoneEntry( "Paul Kratides", "(815)439-9271" );
phoneBook[3] = new PhoneEntry( "Violet Smith", "(312)223-1937" );
phoneBook[4] = new PhoneEntry( "John Smith", "(913)883-2874" );
}
PhoneEntry search( String targetName )
{
for (int j=0; j<phoneBook.length; j++)
{
if ( phoneBook[ j ].
//stuff is to make it so that it only tests if its to upper case and if it contains the target
name.toUpperCase().contains( targetName.toUpperCase()))
return phoneBook[ j ];
}
return null;
}
}
非常感谢任何帮助。
您写道:
count[counter] = l; //this is where the issue lies
其实真正的问题在于:
int[] count = new int[0];
您已经创建了大小为零的数组;也就是说,你不能将任何元素放入到。如果您尝试这样做,您将得到一个例外。大概这就是正在发生的事情......
在Java中,数组的大小是固定的。您以给定的大小创建它们,并且它们将永远保持该大小。