简体   繁体   English

访问Java中的部分数组?

[英]Accessing Partial Arrays in Java?

Hello persons of StackOverflow, 大家好,StackOverflow,

I am having trouble with my current code. 我当前的代码有问题。 I keep getting a null pointer exception when I attempt to see if an array contains a certain search value. 当我尝试查看数组是否包含某个搜索值时,我不断收到空指针异常。 Here is my current code, and here are the criteria for which I have to complete it with. 这是我当前的代码,这是我必须完成的标准。 This is my first post here, so all beneficial criticisms are accepted. 这是我在这里的第一篇文章,因此所有有益的批评都可以接受。

Criteria: Create a program that let's the user enter information into an address book, and then search the names in the address book. 条件: 创建一个程序,让用户在通讯簿中输入信息,然后在通讯簿中搜索名称。 You must store the following in the address book: Name Phone Number Email Address The program will allow the user to enter as many people as they want into the address book, up to 100. The user will enter an empty name to indicate they are done entering names. 您必须在通讯录中存储以下内容:姓名电话号码电子邮件地址该程序将允许用户在通讯录中输入尽可能多的人(最多100个)。用户将输入一个空名表示已完成。输入名称。 After that the user will enter partial names to search for people in the address book. 之后,用户将输入部分名称以在通讯簿中搜索人员。 For full credit this search should find partial names (see screenshot for examples). 为了获得充分的信誉,此搜索应找到部分名称(有关示例,请参见屏幕截图)。 You program does not need to be case insensitive (ie it can be case sensitive). 您的程序不必区分大小写(即可以区分大小写)。 You program should display all the information for all address book entries that match the search criteria. 您的程序应显示与搜索条件匹配的所有通讯簿条目的所有信息。 The results should be in a chart format (see screenshot for example). 结果应为图表格式(例如,参见屏幕截图)。 If the user enters a blank name to search for the program should end. 如果用户输入空白名称以搜索程序,则应结束。 You must use three parallel one dimensional arrays. 您必须使用三个并行的一维数组。 You must use a global constant to set the maximum number of entries in the address book to 100. 您必须使用全局常量将地址簿中的最大条目数设置为100。

package assignments;

import global.Cod;


public class Homework_08 
{
    public static final int MAXIMUM_ENTRIES = 100;

    public static void main(String[] args) 
    {
        String name[] = new String[MAXIMUM_ENTRIES];
        String phoneNumber[] = new String[MAXIMUM_ENTRIES];
        String email[] = new String[MAXIMUM_ENTRIES];

        String searchValue;
        int index = 0;
        boolean match = false;

        if (!name.equals(MAXIMUM_ENTRIES))
        {
            for (index = 0; index < MAXIMUM_ENTRIES; index++) {
                System.out.print("Enter name (Or <ENTER> if done): ");
                name[index] = Cod.next();

                if (!name[index].equals("")) {
                    System.out.print("Enter phone number: ");
                    phoneNumber[index] = Cod.next();

                    System.out.print("Enter email address: ");
                    email[index] = Cod.next();
                } 
                else if (name[index].equals(""))
                {
                    System.out.print("Enter search value: ");
                    searchValue = Cod.next();

                    while (!match && index < MAXIMUM_ENTRIES)
                    {
                        if (name[index].contains(searchValue)) {
                            match = true;
                        }
                        else
                        {
                            index++;
                        }
                    }

                    System.out.println("Name                 Phone          Email");
                    System.out.println("----                 -----          -----");

                    if (match) 
                    {
                        System.out.println(name[index] + phoneNumber[index] + email[index]);
                    }
                    else
                    {
                        System.out.println("                                         ");
                    }
                }
            }
        }
    } //main()
} //class

The Cod import statements are pretty much simpler ways for me to do the scanner. Cod导入语句对我来说是执行扫描仪的简单得多的方法。 They work the same way, where as next() is simply asking for the next string, and every other possibility is self-explanatory. 它们的工作方式相同,其中next()只是简单地询问下一个字符串,其他所有可能性都是不言而喻的。

Thank you. 谢谢。

The problem is that your while (!match && index < MAXIMUM_ENTRIES) loop starts searching at the last entry in your array (ie the current value of index ). 问题是您的while (!match && index < MAXIMUM_ENTRIES)循环从数组的最后一个条目(即index的当前值(!match && index < MAXIMUM_ENTRIES)开始搜索。

The enclosing for() loop updates index every time you get a name, phone, & email. 每次获得名称,电话和电子邮件时,封闭的for()循环都会更新index That's good. 那很好。 But when you search you need to start your search at array index 0 and go to index (ie the current count of how many valid elements are in the array). 但是,当您搜索时,需要从数组索引0开始搜索并转到index (即,数组中有多少个有效元素的当前计数)。

Try breaking out of the data-entry loop when you get an empty name string and making a new loop to search through the names (and only search up to index names). 当您得到一个空的name字符串时,尝试打破数据输入循环,并进行一个新的循环来搜索名称(并且仅搜索index名称)。

Also, you don't need to check for name being empty twice. 另外,您无需两次检查名称是否为空。 This code: 这段代码:

if (!name[index].equals("")) {

tests to see if name is non-empty, yet in the else block that follows you test it again: 测试以查看名称是否为非空,但在随后的else块中再次进行测试:

else if (name[index].equals(""))

Since you already know it is non-empty from the first test, there is no reason to do it again. 由于您已经从第一个测试中知道它是非空的,因此没有理由再次执行它。 Just say else { 只说else {

That's not a bug per-se, just an optimization and a way you need to remember to look at if/else blocks. 这本身并不是一个错误,而只是一种优化以及您需要记住的查看if/else块的方式。

Here I can see two problems 1) you dont have null check for name[index]. 在这里我可以看到两个问题1)您没有对name [index]进行null检查。 As you are initializing array, all the elements initial value will be null. 在初始化数组时,所有元素的初始值将为null。 2) you are using same index for iteration, This may cause new addition of contacts will go to random locations. 2)您使用相同的索引进行迭代,这可能导致新添加的联系人将进入随机位置。 Better use seperate index searchIndex for search iteration. 最好将单独的索引searchIndex用于搜索迭代。

int searchIndex = 0;
while (!match && searchIndex< MAXIMUM_ENTRIES)
                {
                    if (null!=name[searchIndex] && name[searchIndex].contains(searchValue)) {
                        match = true;
                    }
                    else
                    {
                        searchIndex++;
                    }
                }
import global.Cod;
import java.util.*;
import java.io.*;
public class Homework_08 
{
    public static final int MAXIMUM_ENTRIES = 100;

    public static void main(String[] args) 
    {
        String names[] = new String[MAXIMUM_ENTRIES];
        String phoneNumbers[] = new String[MAXIMUM_ENTRIES];
        String emails[] = new String[MAXIMUM_ENTRIES];
        Scanner scan = new  Scanner(System.in);
        String searchValue,name,mail,pno;
        int index = 0,i;
        boolean match = false;

          for (index = 0; index < MAXIMUM_ENTRIES; index++) {
                System.out.print("Enter name (Or <ENTER> if done): ");
                name= scan.nextLine(); 
                if(name=="")break;
                else{
                    System.out.print("Enter phone number: ");
                    pno = scan.nextLine(); 
                    System.out.print("Enter email address: ");
                    mail= scan.nextLine(); 
                    names[index]=name;
                    phoneNumbers[index]=pno;
                    emails[index]=mail;

                }
            }
            System.out.print("Enter name to search (Or <ENTER> if done): ");
            searchValue= scan.nextLine(); 
            while(searchValue!=""){
             for(i=0;i<index;i++){
                if(names[i].contains(searchValue)){
                    System.out.println("Name "+names[i]+"Phone Number "+phoneNumbers[i]+"Email id "+emails[i]);
                }
             }
             System.out.print("Enter name to search (Or <ENTER> if done): ");
             searchValue= scan.nextLine(); 
            }
    } //main()
} //class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM