所以文件中的数据是女孩和男孩的名字。顺序是人气,对应的数字是名字在某一年被注册的次数。例如:
弗兰克 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)