0

I'm trying to store girl and boy names into an array.

I got most of the code except the storing the files into an array.

This is what girls.txt looks like

Emma 125125

Elaina 415545

Kim 545454

Boys.txt:

Devan 45645

Tom 4545

Chris 4879797

i need help storing the names and numbers from files into array boynames array and girlnames array. I show where i need help with comments in code

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Project1Names {
   public static void main(String[] args) {
    Scanner inputStream = null;
    String[][] boynames = new String[1000][2];
    String[][] girlnames = new String[1000][2];
    String line = null;
    boolean isFoundB = false;
    boolean isFoundG = false;
    try {
        inputStream = new Scanner(new FileInputStream("boys.txt"));
    } catch (FileNotFoundException e) {
        System.out.println("Problem opening file boys.txt");
        System.exit(0);
    }

    Scanner inputStreamGirls = null;
    try {
        inputStreamGirls = new Scanner(new FileInputStream("girls.txt"));
    } catch (FileNotFoundException e) {
        System.out.println("Problem opening file girls.txt");
        System.exit(0);
    }
       int count = 0;
        while (count < 1000){
            inputStream =  boynames[count][0]; //Error here
            inputStream =  boynames[count][1]; //here
            count++;
        }

        count = 0;

        while (count < 1000 ){
            inputStreamGirls = girlnames[count][0]; //here
            inputStreamGirls = girlnames[count][1]; //here
            count++;
        }
      Scanner keyboard = new Scanner(System.in);
      System.out.println("Enter the first name that you would like to find the popularity of.\n Be sure to capitalize the first letter of the name.\n");
      String answer = keyboard.next(); 

        count = 0;
        while(count < 1000){
            if (boynames[count][0] == answer){
                System.out.println(boynames[count][0] + " is ranked " + count + " among boys with " +  boynames[count][1] +  " namings");
                isFoundB = true;
            }
            if (girlnames[count][0] == answer){
                System.out.println(girlnames[count][0] +  " is ranked " + count +  " among girls with " + girlnames[count][1] + " namings");
                isFoundG = true;
            }
            count++;
        }

        if(isFoundB == false){
            System.out.println(answer + " is not ranked among the top 1000 boy names.");
        } 
        if(isFoundG == false){
            System.out.println(answer + " is not ranked among the top 1000 girl names.");
        }

    inputStreamGirls.close();
    inputStream.close();
    keyboard.close();
}
}

1 Answers1

1

You will need to call the scanner methods to actually read from the input file.

scanner.next() reads one string token from the input.

So instead of this part:

inputStream =  boynames[count][0]; //Error here
inputStream =  boynames[count][1]; //here

You would do:

boynames[count][0] = inputStream.next();
boynames[count][1] = inputStream.next();
Hesham Attia
  • 979
  • 8
  • 13
  • Thank you. Don't know why i didnt think about that. That is how I would store anything to a variable. don't know why I thought differently. Had to add it too look like this for any one looking at this code for future reference. while (count < 1000 && inputStream.hasNextLine()) { boynames[count][0] = inputStream.nextLine(); boynames[count][1] = inputStream.nextLine(); count++; } – MoneyMonster Feb 25 '17 at 20:04
  • One problem is that it is always returning false and not finding the names. any idea why? – MoneyMonster Feb 25 '17 at 20:13
  • you're using `nextLine` and not `next`, this makes you save the whole line in one variable without splitting it, so `boynames[count][0]` would be equal to `Devan 45645`, instead of just `Devan`. You should use next as in the example in my answer. – Hesham Attia Feb 25 '17 at 20:15
  • i changed it, but its still always returning false it skips the second while loop and jumps to if statement saying its not found – MoneyMonster Feb 25 '17 at 20:22
  • I think the problem is in `boynames[count][0] == answer`. You can't use == to compare strings, you need to do `boynames[count][0].equals(answer)` instead. See http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java for more info. – Hesham Attia Feb 25 '17 at 20:29
  • Thank you for time – MoneyMonster Feb 25 '17 at 20:48
  • It did thanks. Tried to do a do-While loop so it could ask the user for another input, but i would get a nullpointexception. @ line 51. if (boynames[count][0].equals(answer)) every thing even if its a girl name. Got any idea why? – MoneyMonster Feb 27 '17 at 00:30