0

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)

Ange King
  • 147
  • 1
  • 2
  • 12
  • What is really your question ? I see a description but no real question. Usually the policy for homework is you do it and ask on SO when you are stuck on a specific question. – HpTerm Aug 15 '13 at 11:11
  • meant to add: that if no match name found, then print 1000. I guess I don't really have a direct question. Is that not allowed? my current code is just returning 1000, 1000. so I'm not sure where I'm going wrong, I would just like to get some suggestions basically. sorry, i will delete if this isn't allowed... – Ange King Aug 15 '13 at 11:15
  • Seems you are a student. It is important you learn things. At least that's my point of view. The better is trying to do the job yourself. Then when you face a problem. Try to solve it with standard tools (debugger) and your knowledge. Then only share the problem with other to understand it and solve it. – HpTerm Aug 15 '13 at 11:17
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) Well, it's not really a duplicate, but you're not really asking anything and that the (or 'a') problem with your code. – Bernhard Barker Aug 15 '13 at 11:25
  • ok, thanks - i think... I have written all of that code myself, so I'm not sure what you mean by 'trying to do the job yourself' - I have tried and now I need help... anyway, no worries, if I am going to get lectured on asking for help with understanding, maybe I wont bother... – Ange King Aug 15 '13 at 11:26
  • Now it's cool you ask for the question with that 1000, 1000. But you should edit your post and say it clearly. I answered you. – HpTerm Aug 15 '13 at 11:30
  • 1
    A few tips for future question - you should include test input, expected output and actual output in your question. You should preferably debug your code first to get to the core of the issue, and post an [SSCCE](http://sscce.org/) highlighting this problem. You should definitely have a question, even if it is "Why am I not getting the correct output?". Also, feel free to edit the question; this should be done to add additional information required to answer the question, as opposed to using comments for this. – Bernhard Barker Aug 15 '13 at 11:32
  • @Dukeling I agree with you. The real question/problem was asked in the comments. This must be stated in the question and not in the comments. – HpTerm Aug 15 '13 at 11:33
  • sorry everyone, i actually tried to edit the post, but 'it' wouldn't let me, so added it to comments. I'm new to this, thanks for all your advice. – Ange King Aug 15 '13 at 13:11
  • Still a little confused as to what the exact issue is; you say that you're getting `1000, 1000` in the question description, but there's also a NullPointerException stack trace. Is it possible to edit the question to clearly say which it is? – Dennis Meng Dec 30 '13 at 03:33

2 Answers2

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++)
HpTerm
  • 8,151
  • 12
  • 51
  • 67
  • @AngeKing If I solved your problem think of validating my answer so others know your problem has been solved. – HpTerm Aug 15 '13 at 12:02
  • sorry, it didn't solve my problem. I got a 'nullpointerexception' so I am researching what that actually means, and how i can fix it, maybe with a try/catch? anyway, thanks for your help. i really appreciate it :-) – Ange King Aug 15 '13 at 13:13
  • ps: shall i validate it anyway? – Ange King Aug 15 '13 at 13:13
  • @AngeKing Update your question and post the stack trace so I can read where the error is for your NPE (nullPointerException) – HpTerm Aug 15 '13 at 13:41
  • line 21 is: if (entries[i].equals(target)) line 64 is: System.out.print(isInArray(girlNames, getName) + countG[isInArray(girlNames, getName)-1]); – Ange King Aug 15 '13 at 13:54
  • @AngeKing forget my previous comment (I deleted it). I was mixing stuff between different languages. Coming back to your problem, use the debugger to go step by step in your code and check if girlNames and other variables are filled correctly. Is your loadFile function working correctly ? – HpTerm Aug 15 '13 at 14:54
-1

Try that instead:

    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;
    }

I don't know if it solves all the issues but that's a beginning ;)

Barbe Rouge
  • 394
  • 1
  • 6
  • 18
  • `number = i + 1;` when first time entered, whould mean `number = 1000 + 1;` not as it was before: `number = 0 + 1;`... you've solved one thing and broke other one that was fine. – dantuch Aug 15 '13 at 11:32
  • number = 0 was used to initialize number but came out as ENTRIES if the target was not found, isn't it was it does with my modified piece of code? – Barbe Rouge Aug 15 '13 at 11:47