Hi, Let me give you guys a little overview of my problem here without going into huge detail and confusing anyone.
I got a little bonus program im trying to write for one of my java classes this semester.
The program needs to read in girls/boys names from the girlsnames.txt and boysnames.txt files and store then in an array. Then allow the user to input a name and then take that input and scan both the girlsnames array and boysnames array for that name, Return the name along with the number of how many of this name was used in 2003.
Here's what a few lines in the input files look like. There's a name seperated by a space then a number representing how many of this name was used in the year 2003.
Here are the first five girlsnames from girlsnames.txt. boysnames.txt is exactly the same except obviously boys names. :)
Emily 25494
Emma 22532
Madison 19986
Hannah 17393
Olivia 15982
Well simple really and Ive managed to get my program to find the maximum number of names in the file which it used as a constant for the array. (so if you add more to the file my program will know and set the limit for the array to match that.) Ive also managed to store all the boys and girls names/numbers in respective arrays as Strings. Ive also got the scanner class set up to recive the input. Where my programing skills fail me is when I need to compare the user's input with the arrays and have some method scan the arrays looking for the name that the user inputed, then return that name along with its index and the number of times that name was used in 2003.
If anyone fells like droping a little hint or some information to help that be great. Like I said its a bonus program so ive already done the regular program without problems and its not a big deal if I get this done or not. Just that I would like to learn how to do this even if it is bonus. Anyways here is my code. It may be a little slopy so just let me know if I can make anything more clear.
BTW I choped it down so it will compile and actually do some things. It will ask for the name to input then once you input it will print out the limit for boys and limit for the girls(ARRAY) and an index of girls and boys array just to show that those things are working.
I was thinking of using the stringtokenizer class but after reading up on it for 2 to 3 hours I couldnt really comprehend it enough to implement it here. Also I know that the .slit method can be used as well but again im not familiar with it enought to implement it.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.io.FileInputStream;
public class BoysAndGirls2
{
public static void main (String[] args)
{
try
{
String lineBoyLimit = null;
String lineGirlLimit = null;
String lineBoys = null;
String lineGirls = null;
int LIMITBOY = 0;
int LIMITGIRL = 0;
int lineNumber = 0;
BufferedReader inputStreamBoy = new BufferedReader(new FileReader("boynames.txt"));
BufferedReader inputStreamGirl = new BufferedReader(new FileReader("girlnames.txt"));
Scanner inputStreamBoy1 = new Scanner(new FileInputStream("boynames.txt"));
Scanner inputStreamGirl1 = new Scanner(new FileInputStream("girlnames.txt"));
int index = 0;
do
{
lineBoyLimit = inputStreamBoy.readLine();
if (lineBoyLimit == null)
{
LIMITBOY = lineNumber;
inputStreamBoy.close();
break;
}
lineNumber++;
}while (!lineBoyLimit.equals(null));
lineNumber = 0;
do
{
lineGirlLimit = inputStreamGirl.readLine();
if (lineGirlLimit == null)
{
LIMITGIRL = lineNumber;
inputStreamGirl.close(); break;
}
lineNumber++;
}while (!lineGirlLimit.equals(null));
String[] boys = new String[LIMITBOY];
String[] girls = new String[LIMITGIRL];
while (inputStreamBoy1.hasNextLine())
{
boys[index] = inputStreamBoy1.nextLine();
index++;
}
index = 0;
while (inputStreamGirl1.hasNextLine())
{
girls[index] = inputStreamGirl1.nextLine();
index++;
}
Scanner scan = new Scanner(System.in);
System.out.println("Enter a name ");
String name = scan.nextLine();
System.out.println("LIMITGIRLFOUND = " + (lineNumber));
System.out.println("LIMITBOYFOUND = " + (lineNumber));
System.out.println("Boys index 10 = " + boys[10]);
System.out.println("Girls index 10 = " + girls[10]);
}
//**********************************
// The Catch block. Catches the FileNotFoundException and prints the text.
//**********************************
catch(FileNotFoundException e)
{
System.out.println("File input.txt was not found or could not be opened ");
System.out.println("or file output.dat coud not be created");
}
//**********************************
// The Catch block. Catches the IOException if the file cannot be written to or read from.
//**********************************
catch(IOException e)
{
System.out.println("Error writing to output.dat or reading from input.txt");
}
}
}