i want Use the 2 text files boynames.txt and girlnames.txt Prompt the user for boy or girl and then for a letter from the alphabet. Open either boynames.txt or girlnames.txt and read each line in – when the name starts with the letter that the user specified then write the name and the number to an output file. And I want it output file an appropriate name such as boyJ.txt or girlA.txt. boyJ.txt will look something like this:
Jacob 29195
Joshua 24950
Joseph 21265
-------------------
i have a problem with the code , i want to input of the boy name that start from " J " like example above but it does not work. and it show all the name . so how can i write the code that input the frist letter of the name and it show all the name that start with that letter ?

so i hope anyone can help me thank.

thiis my code

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

public class demo
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Scanner inputStream= null;
String line = null;
System.out.println("Please enter name:");
line = keyboard.nextLine( );

try
{
inputStream =
new Scanner(new FileInputStream("boynames.txt"));
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file ");
System.exit(0);
}
while(inputStream.hasNextLine()){


line=inputStream.nextLine();
System.out.println("boy name "+ line );
}

inputStream.close( );

}
}

Well the most important thing to remember is the importance of the Java library. There are many methods that you can use, and probably the most important is the charAt() method. The charAt() method can be used to compare a character in one string to a character in another string. Here are some steps to go about your problem:

1) Create an if-else statement that would compare whether the user types boy, girl, or some invalid input. So, it would look something like, :

if (input.equalsIgnoreCase("boy"))
              //open up boynames.txt
              //Prompt user for letter (ex. "J")
              //read in each line, line by line, and compare the first letter to the letter that the user inputted
             //if it's a match, write that line to an output file...

           else if (input.equalsIgnoreCase("girl"))
              //same stuff as above
           else
              System.out.println("Invalid input!");

2) Make sure to do error-checking! If the user inputs an invalid character, use a try-catch statement or just use a simple if-else statement to tell the user the input is invalid.

This assignment doesn't seem too difficult...The hardest part might be getting the input/output stuff to work. The pseudocode above should help get the basic stuff down pat. If you have any problems, post your code and we'll try to help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.