0

2 つのファイルから読み取るプログラムを作成する必要があります。これらのファイルには、名前と ID のリストが含まれており、両方とも既に姓に基づいてアルファベット順に並べられています。次に、プログラムは、2 つのプログラムからのすべての名前と ID の完全なリストを含み、それらをアルファベット順に並べた、ユーザーが名前を付けた 3 番目のファイルを作成すると想定されます。ファイルの形式はそのままです--

姓,名
ID(ex.rbb091020)
姓,名
ID

私のプログラムは 2 つのファイルを読み取り、3 番目のファイルを作成しますが、何らかの理由で 3 番目のファイルには何も書き込まず、ファイルは空白のままです。これを修正するにはどうすればよいですか?

これが私のコードです...

import java.io.*;
import java.util.Scanner;

public class Combine_Files
{
public static void main(String[]args) throws IOException
{
    String filename1 = "";
    String filename2 = "";
    String combinedFileName = "";
    String response = "";
    String nextName1 = "";
    String nextName2 = "";
    String nextIDNumber1 = "";
    String nextIDNumber2 = "";
    boolean okToOverwrite = false;
    Scanner keyboard = new Scanner(System.in);

    System.out.println("What is the name of the first file?");
    filename1 = keyboard.nextLine();

    File file1 = new File(filename1);

    if (!file1.exists())
    {
        System.out.println(file1 + " does not exist.\nTerminating Program.");
        System.exit(0);
    }

    System.out.println("What is the name of the second file?");
    filename2 = keyboard.nextLine();

    File file2 = new File (filename2);
    if (!file2.exists())
    {
        System.out.println(file2 + " does not exist.\nTerminating Program.");
        System.exit(0);
    }   

    System.out.println("What is the name of the file that you wish to create?");
    combinedFileName = keyboard.nextLine();
    File combinedFile = new File (combinedFileName);

    while (combinedFile.exists() && !okToOverwrite)
    {
        System.out.println("\nError, a file named " +
            combinedFileName + " already exists." +
            "\nWould you like to overwrite this file?" +  
            "\nEnter Y for Yes or N for No.");
        response = keyboard.nextLine();

        // Add input validation on response

        if (response.equalsIgnoreCase("Y"))
        {
            okToOverwrite = true;
        }
        else 
        {
            System.out.println("Enter a new filename.");
            combinedFileName = keyboard.nextLine();
            combinedFile = new File(combinedFileName);
        }
    }

    if (file1.exists() && file2.exists())
    {

        Scanner list1 = new Scanner(file1);
        Scanner list2 = new Scanner(file2);
        PrintWriter outputFile = new PrintWriter(combinedFile);

            while (list1.hasNext() && list2.hasNext())
            {
                nextName1 = list1.nextLine();
                nextName2 = list2.nextLine();

                if(nextName1.compareToIgnoreCase(nextName2) <0)
                {
                    outputFile.print(nextName1);
                    nextName1 = list1.nextLine();
                    outputFile.print(nextName1);
                }
                else if(nextName1.compareToIgnoreCase(nextName2) >0)
                {
                    outputFile.println(nextName2);
                    nextName2 = list2.nextLine();
                    outputFile.println(nextName2);
                }
                else
                {
                    outputFile.println(nextName1);
                    nextName1 = list1.nextLine();
                    outputFile.println(nextName1);
                    outputFile.println(nextName2);
                    nextName2 = list2.nextLine();
                    outputFile.println(nextName2);
                }       
            }

            while (list1.hasNext() && !list2.hasNext())
            {
                outputFile.println(nextName1);
            }

            while (list2.hasNext() && !list1.hasNext())
            {
                outputFile.println(nextName2);
            }
    outputFile.close();             
    }
}   
}   
4

1 に答える 1

1

私は 2 つのエラーを見つけました: 主な 1 つは、他の入力ファイルが使い果たされた後に 1 つの入力ファイルの行を出力する下部のループが、読み取り中のファイルを進めないことです。したがって、それらが実行されると、それらは無限にループします。

2 番目のエラーは、最初の名前の比較後に「println()」ではなく「print()」を使用したことです。これは行末なしで行を出力しますが、おそらくあなたが望むものではありません。

その後、出力を得ましたが、他のバグについては調べていません。

私が提案する場合:一般的な機能を実行するメソッドをさらにいくつか使用すると、プログラムをより単純にすることができます。例: 1 つのメソッドは、既に読み取られた入力、1 つの入力スキャナ、および出力ファイルを引数として取り、現在の名前を書き出すことができます。何かのようなもの

private void writeName(String currentLine, Scanner inFile, File outFile)
{
  outFile.println(currentLine);
  String nextLine = inFile.nextLine();
  outFile.println(nextLine);
}

これらを特定する方法は、同じコーディング手順を複数回実行しているように見える場合に注意することです。この場合、上記の手順を 2 つの入力ファイルに対して別々に実行しているため、この方法 (および他の同様の方法) を使用すると、コードの「コピー アンド ペースト」の性質を排除できます。

于 2012-04-07T21:02:02.883 に答える