So the data in the files are girls & boys names. The order is the popularity and the corresponding number is the number of times the name was registered in a certain year. eg:
Frank 678
William 2
etc
There are 1000 names in each file. I need to load each file into string[]
& int[]
, then when a name is written into the keyboard, the program must go thou the arrays, find the name, print out the index of the string array (less one obviously) & the number that is in the int[]
. my current code is just returning 1000, 1000. so I'm not sure where I'm going wrong
Hope this makes sense, its for a uni question. Here is my code so far, sorry if it seems primitive, I'm still learning :-)
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)