0

所以文件中的数据是女孩和男孩的名字。顺序是人气,对应的数字是名字在某一年被注册的次数。例如:

弗兰克 678
威廉 2

每个文件中有 1000 个名称。我需要将每个文件加载到string[]&int[]中,然后当一个名字被写入键盘时,程序必须去你的数组,找到名字,打印出字符串数组的索引(显然少一个)&里面的数字int[]. _ 我当前的代码只是返回 1000、1000。所以我不确定我哪里出错了希望这是有道理的,它是一个 uni 问题。到目前为止,这是我的代码,对不起,如果它看起来很原始,我还在学习:-)

import java.io.*;
import java.util.*;

public class H252{

    static final int ENTRIES = 1000; // Number of entries in the file

    public static int isInArray(String[] entries, String target) {

        int number = 0;

        for (int i = 0; i < entries.length - 1; i++)
            if (entries[i] == target)
                number = i + 1;
            else
                number = ENTRIES;

        return number;
    }

    public static void LoadFile(String[] entries, int[] count, String filename) {

        Scanner file = new Scanner(filename);
        int a = 0;
        int b = 0;

        while (file.hasNextLine()) {
            if (file.hasNext())
                entries[a++] = file.next();

            if (file.hasNextInt())
                count[b++] = file.nextInt();
        }

    }

    public static void main(String[] args) {
        while (JPL.test()) {

            Scanner kb = new Scanner(System.in);
            String getName = kb.next();

            String[] boyNames = new String[ENTRIES];
            String[] girlNames = new String[ENTRIES];
            int[] countB = new int[ENTRIES];
            int[] countG = new int[ENTRIES];

            LoadFile(girlNames, countG, "girlnames.txt");
            System.out.print(isInArray(girlNames, getName)
                    + countG[isInArray(girlNames, getName) - 1]);
            LoadFile(boyNames, countB, "boynames.txt");
            System.out.print(isInArray(boyNames, getName)
                    + countB[isInArray(boyNames, getName) - 1]);

        }
    }
}

java.lang.NullPointerException
at H252.isInArray(H252.java:21)
at H252.main(H252.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

4

2 回答 2

1

Your problem is that you compare strings with ==

That way of doing you compare the references and therefore it is always different and you reach the max value and return ENTRIES.

To compare strings in java use .equals

in your code replace

if (entries[i] == target)

with

if (entries[i].equals(target))

and it should work.

EDIT

Another mistake is that with

for (int i = 0; i < entries.length - 1; i++)

you never check your last element of entries. Correct your code to

for (int i = 0; i < entries.length; i++)
于 2013-08-15T11:24:00.360 回答
-1

试试这个:

    public static int isInArray(String[] entries, String target) {
        int number = ENTRIES;
        for (int i = 0; i < entries.length; i++){  // You were not checking the last element of the array
            if (entries[i].equals(target)){  //if you want to compare String use method equals
                number = i + 1;
            }  // With the previous else comment you were almost certain to always get ENTRIES value returned all the time
        }
        return number;
    }

我不知道它是否解决了所有问题,但这是一个开始;)

于 2013-08-15T11:29:28.863 回答