1

这是给我的提示“编写一个程序并使用附件(babynames.txt)作为输入文件,并创建两个输出图块。一个文件列出所有男孩的名字,另一个文件列出所有女孩的名字”

这是我到目前为止所拥有的

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();
 }
}

我假设我需要将文本文件中的数据输入到数组中。每个令牌由一个空格分隔。我不知道如何处理这个问题。此外,在设置数组后,我是否需要将out.printf()and更改out2.printf()为 if 循环,以将我想要的令牌放入它们各自的 txt 文件中。

输入 txt 文件中的示例行

1 Michael 462085 2.2506 Jessica 302962 1.5436

4

0 回答 0