PDA

View Full Version : Can someone fix this for me!?


alexman27
Dec 1, 2014, 10:46 PM
Can someone please! Fix this for me?
I need to write a program that reads from a text file of popular girl names and a text file of popular boy names.The user should be able to enter a boy's name, a girl's name, or both, and the application will display messages indicating whether the names were among the most popular.
This is what I have until now.. Thank you for your time : )
import java.io.*;
import java.util.Scanner;
public class NamesSearch {




public static void main(String[] args) throws FileNotFoundException {
File BoysNamesfile = new File ("BoysNames");
Scanner inputFile = new Scanner(BoysNamesfile);


File GirlsNamesfile = new File ("GirlsNames");
Scanner inputFile2 = new Scanner(BoysNamesfile);


String[] BoysNamefile = BoysNamesfile.list();
String[] GirlsNamefile = GirlsNamesfile.list();


Scanner keyboard = new Scanner(System.in);


int choice;
String input = null;
String name;






System.out.println("Enter 1 if you would like to check a boys name in the popular name list, or enter 2 if you would like to+" +
"check for a girls name.");
choice = keyboard.nextInt();
if(choice == 1)
{
System.out.println("Enter the name you would like to check.");
name = keyboard.nextLine();
inputFile = sequentialSearch(BoysNamefile, name);
if (BoysNamesfile.list = name )
{
System.out.println("The name is on the popular list.");
}
else
{
System.out.println("The name is not on the popular list.");
}
}












}


private static Scanner sequentialSearch(String[] boysNamefile, String input) {
// TODO Auto-generated method stub
return null;
}


}

CravenMorhead
Dec 2, 2014, 09:27 AM
First off you're opening the boy's name file twice, once as the boys scanner, and once as the girls scanner.

Also:

if (BoysNamesfile.list = name ) is an assignment not a equality test. IE, you're setting BoysNamesfile.list to name, not checking to see if they're the same.

I am primarily a C/C++ programmer, but there's something hinky with the way you're using the scanners and the input files. I neither have the time nor the will right now to look up and re-learn Java, but I would look at your Java book on examples of how to use those classes because I think you got them wrong. Be careful on your naming and the use of it, you can generate bugs by just copy and pasting without changing. As well, make sure you debug as you go, for example your search method is undefined but you'll probably get the code around it working before defining it. If there's a bug you won't know if it is in the main part or the method. Build the method, test that it's solid then move on and put it all together. Be VERY conscious of your syntax too.

Good luck.

alexman27
Dec 2, 2014, 12:27 PM
First off you're opening the boy's name file twice, once as the boys scanner, and once as the girls scanner.

Also:
is an assignment not a equality test. IE, you're setting BoysNamesfile.list to name, not checking to see if they're the same.

I am primarily a C/C++ programmer, but there's something hinky with the way you're using the scanners and the input files. I neither have the time nor the will right now to look up and re-learn Java, but I would look at your Java book on examples of how to use those classes because I think you got them wrong. Be careful on your naming and the use of it, you can generate bugs by just copy and pasting without changing. As well, make sure you debug as you go, for example your search method is undefined but you'll probably get the code around it working before defining it. If there's a bug you won't know if it is in the main part or the method. Build the method, test that it's solid then move on and put it all together. Be VERY conscious of your syntax too.

Good luck.
THank you for your time and input !