This is the prompt I was given "Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name"
Here is what I have so far
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class BabyNames
{
public static void main(String[] args) throws FileNotFoundException
{
// Prompt for the input and output file names
Scanner console = new Scanner(System.in);
System.out.println("Input file: ");
String inputFileName = console.next();
System.out.println("Output file 1: ");
String outputBoysNames = console.next();
System.out.println("Output file 2: ");
String outputGirlsNames = console.next();
// Construct the Scanner and PrintWriter objects for reading and writing
File inputFile = new File(inputFileName); // creates variable that reads from txt
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter(outputBoysNames); // writes to output file (variable name)
PrintWriter out2 = new PrintWriter(outputGirlsNames);
// Read the input and write the output
while (in.hasNextLine()) {
String A = in.next();
String B = in.next();
String C = in.next();
String D = in.next();
String E = in.next();
String F = in.next();
String G = in.next();
out.printf(B);
out2.print(E);
}
in.close(); // Prevents this file is being used error
out.close();
out2.close();
}
}
I would assume that I would need to input the data from the text file into an array. Each token is separated by a space. I don't know how to approach this. Also, after the array is set up would I need to change the out.printf()
and out2.printf()
to an if loop to put the tokens I want into their respective txt file.
Example line from input txt file
1 Michael 462085 2.2506 Jessica 302962 1.5436