Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java搜索是否存在字符串_Java_Arrays_Search - Fatal编程技术网

Java搜索是否存在字符串

Java搜索是否存在字符串,java,arrays,search,Java,Arrays,Search,我试图确定用户输入的值是否已经存在于当前数组中,如何做到这一点 用户输入的值用于检查变量是否为 accno和要比较的数组是accnums 这就是我目前正在进行的工作 public class Randomtopic { static BigDecimal[] accbal = new BigDecimal[20]; static Integer[] accnums = new Integer[20]; public static void main(String[]

我试图确定用户输入的值是否已经存在于当前数组中,如何做到这一点

用户输入的值用于检查变量是否为 accno和要比较的数组是accnums

这就是我目前正在进行的工作

public class Randomtopic {

    static BigDecimal[] accbal = new BigDecimal[20];
    static Integer[] accnums = new Integer[20];

    public static void main(String[] args) {
        displayMenu();
    }

    public static void displayMenu() {
        int option, accno;
        double accbal;
        Scanner sc = new Scanner(System.in);
        System.out.println(" Add an account");
        System.out.println("Search an account with the given account number");
        System.out.print("Enter Your Choice: ");
        option = sc.nextInt();
        switch (option) {
            case 1:
                System.out.println("You have choosen to add account");
                addAccount();
                break;
            case 2:
                System.out.println("You have choosen to search for an account");
                System.out.print("Enter the Account Number: ");
                accno = sc.nextInt();
                System.out.println(search(accno, accnums));
                break;
            default:
                System.out.println("Please choose an appropriate option as displayed");
        }
        displayMenu();
    }

    public static void addAccount() {
        //String accno;
        int i = 0;
        int accno, input;
        BigDecimal accbala;
        DecimalFormat df = new DecimalFormat("0.00");
        //BigDecimal[] accbal= new BigDecimal[20];
        Scanner sc = new Scanner(System.in);
        //String[] accnums = new String[20];
        int j;
        System.out.print("Enter the account number: ");
        accno = sc.nextInt();
        if (String.valueOf(accno).matches("[0-9]{7}")) {
            System.out.print("Enter account balance: ");
            accbala = sc.nextBigDecimal();
            for (j = 0; j < accnums.length; j++) {
                if (accnums[j] == null )
             break;
         else if(accnums[j].equals(accno))
         {
              System.out.println("Account already exists");
         }
            }
            //System.out.print(j);
            if (j == accnums.length) {
                System.out.print("Account storage limit has reached.");

            } else {

                    accnums[j] = accno;
                    accbala = accbala.setScale(2, RoundingMode.HALF_UP);
                    accbal[j] = accbala;

            }
            input = accnums[0];
            System.out.println("The value: " + input + " witha a balance of " + accbal[0].toString());
        } else {
            System.out.println("Wrong NRIC");
        }
        displayMenu();
    }

    public static String search(int accnum, Integer[] numbers) {
        // Integer[] numbers;
        //int key;
        //numbers = accnums;
        // System.out.print("Enter the Account Number: ");        
        for (int index = 0; index < numbers.length; index++) {
            if (numbers[index].equals(accnum)) {
                return String.valueOf(index);
                // System.out.println("The account number exists in this array index :"+index);//We found it!!!
            }
            break;
        }
        return "-1";
    }
}
公共类随机主题{
静态BigDecimal[]accbal=新的BigDecimal[20];
静态整数[]accnums=新整数[20];
公共静态void main(字符串[]args){
显示菜单();
}
公共静态无效显示菜单(){
int选项,accno;
双accbal;
扫描仪sc=新的扫描仪(System.in);
System.out.println(“添加帐户”);
System.out.println(“搜索具有给定帐号的帐户”);
System.out.print(“输入您的选择:”);
option=sc.nextInt();
开关(选件){
案例1:
System.out.println(“您已选择添加帐户”);
addAccount();
打破
案例2:
System.out.println(“您已选择搜索帐户”);
系统输出打印(“输入账号:”);
accno=sc.nextInt();
System.out.println(搜索(accno,accnums));
打破
违约:
System.out.println(“请选择显示的适当选项”);
}
显示菜单();
}
公共静态无效addAccount(){
//字符串accno;
int i=0;
int accno,输入;
大十进制阿克巴拉;
DecimalFormat df=新的DecimalFormat(“0.00”);
//BigDecimal[]accbal=新的BigDecimal[20];
扫描仪sc=新的扫描仪(System.in);
//字符串[]accnums=新字符串[20];
int j;
系统输出打印(“输入账号:”);
accno=sc.nextInt();
if(String.valueOf(accno).matches(“[0-9]{7}”)){
系统输出打印(“输入账户余额:”);
accbala=sc.nextBigDecimal();
对于(j=0;j
那我的问题呢?
当我第一次输入accnum本身时,我得到的是NullPointerException。Tks

您的
账户
中填充了
null
值。检查它们是否不是
null
。我假设您的
NullPointerException
是从
search
方法抛出的。

如果我正确理解了您的代码,您有许多账户,其相关余额由账号标识。在这种情况下,我将使用一个映射,而不是摆弄数组。

当您第一次运行方法搜索时,您的数组
accnums
中填充了空值,因此当您调用line
if(numbers[index].equals(accnum))
时,您正在尝试对空对象调用equal方法


您可以将
帐户从数组更改为列表,然后大小将取决于数字元素,因此,它不会像您的数组那样具有固定大小

如果不在您正在搜索的accnum中添加任何内容,代码中的问题就会出现。因此,您的accnum包含空值。如果将accnum数组更改为list/hashmap,则不会出现异常。

当您实例化或创建数组时,默认情况下,这些值设置为空值;每当您迭代一个可能包含空值的数组时,都需要跳过这些值。比如说,

for(loop conditions)
        if (numbers[index] != null && (numbers[index].equals(accnum))) {
            return String.valueOf(index);
            //your text goes here;
        }
        break;//I suggest you take this line out of your original code. Your for loop will end this once you have iterated through the entire array, or you will automatically break out if you find the value.
}
如果必须使用数组,那么这是遍历它的最佳方法。if子句中的括号和&&阻止您执行.equals检查numbers[index]==null的值,从而阻止抛出null错误。唯一的选择是将数字[]中的每个值设置为0或其他值,然后在迭代时跳过该值。这将使用静态final int完成。但是,这不是理想的编码


理想情况下,您应该使用ArrayList,这样不仅可以提高效率,还可以提高可读性

@Baz,在哪里?我没有在
search
method中看到类似于null的检查。他有吗?这:number[index].equals(accnum)可能是问题所在。@Baz,他说当他输入账号时会抛出异常。除非这是
[家庭作业]
我会使用
列表
或者在这种情况下使用
映射
,这将简化代码的分配,并使其稍微快一点,有一半以上的代码是不需要的。@PeterLawrey这确实是家庭作业,我必须只使用我所学到的东西..tks任何与此相关的建议都会对我有很大帮助tksI建议在d