1

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

Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
x65748
  • 11
  • 2
  • You shouldn't need an array, simply write to either output file while reading from the input file. How are you supposed to differentiate between a boys name and a girls name? Do you have a master list of names specifying if a name is a boys name or girls name? – Jonny Henly Oct 25 '20 at 21:57
  • @JonnyHenly my teacher has specified the tokens that are boys or girls names. When I run my code at the moment i get the error. Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at BabyNames.main(BabyNames.java:33) That is why I thought I would need to use an array. – x65748 Oct 25 '20 at 22:05
  • 1
    Please edit your question and provide an example of a line from the input file. I have no idea what’s on each line, and variable names like A, B, C, D, E, F, and G don’t do much to clarify it. – VGR Oct 25 '20 at 22:05
  • @VGR I provided a line from the txt file sorry that slipped my mind – x65748 Oct 25 '20 at 22:08
  • So each line can contain more than one name (i.e. `Michael` and `Jessica`)? Also what do the numbers indicate? Why are there numbers if you just need to separate boys names from girls names? – Jonny Henly Oct 25 '20 at 22:16
  • @JonnyHenly that is the txt file I am pulling from. Then make two new txt files one with only boys names String B and one with only girls names which is string E – x65748 Oct 25 '20 at 22:18
  • How is your code supposed to know which names are boy names and which names are girl names? Is the first name a boy name on every line in the input file? Is the second name a girl name on every line in the input file? – VGR Oct 25 '20 at 22:57
  • @VGR yes it follows the same pattern as the example I gave – x65748 Oct 25 '20 at 23:09
  • It would seem that your only problem is separating the names in your output files. Try changing out.printf to `out.println` and out2.print to `out2.println`. – VGR Oct 25 '20 at 23:24

0 Answers0