Logi c ошибка чтения только одной строки при использовании файлов в java - PullRequest
1 голос
/ 13 июля 2020

У меня есть задание, в котором мне нужно было создать программу с двумя файлами, в первом хранится набор строк, а во втором - копия этих строк, но только в верхнем регистре.

I успешно создал программу, однако, когда мой профессор оценил мое задание, он снял баллы, так как "Произошла ошибка logi c при чтении ТОЛЬКО одной строки" из этой строки кода:

 String first_file_string = first_file_input.nextLine();

Я не совсем уверен, что это означает или как это исправить, любой вклад или помощь будут очень благодарны. Спасибо за ваше время и внимание!

Вот мой весь код, а строка выше - это строка 50, я поставлю 3 * до и после строки.

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


public class UppercaseFileConverter
{
    public static void main(String[] args) throws IOException
    {
        Scanner keyboard = new Scanner(System.in);
        // file creation and writing string of characters to the file
        System.out.print("Enter a file name to create the first file to read from: ");
        String file_one = keyboard.nextLine();
        PrintWriter outputFile_one = new PrintWriter(file_one);

    System.out.print("Enter a string of characters to store in the first file: ");
    String user_char = keyboard.nextLine();
    outputFile_one.println(user_char);
    outputFile_one.close();
    System.out.printf("\nThe file: %s has been generated.\n\n", file_one);

    System.out.print("Enter a file name to create the second file to write into: ");
    String file_two = keyboard.nextLine();
    PrintWriter outputFile_two = new PrintWriter(file_two);
    outputFile_two.close();
    System.out.printf("\nThe file: %s has been generated.\n", file_two);

    // Opening the first file
    System.out.print("\nEnter the first file name to read from: ");
    String first_file = keyboard.nextLine();

    // reading the first file
    File open_first = new File(first_file);
    if (!open_first.exists())
    {   // If file doesn't exist, program ends
        System.out.printf("ERROR: File, %s, cannot be found.\n", first_file);
        System.out.println();
        System.exit(0);
    }
    else
    {
        // accessing contents of first file
        Scanner first_file_input = new Scanner(open_first);
        ***String first_file_string = first_file_input.nextLine();***

        // opening the second file
        System.out.print("\nEnter the second file name to write into: ");
        String second_file = keyboard.nextLine();

        // reading the second file
        File open_second = new File(second_file);
        if (!open_second.exists())
        {   // If file doesn't exist, program ends
            System.out.printf("ERROR: File, %s, cannot be found.\n", second_file);
            System.out.println();
            System.exit(0);
        }
        else
        {   // storing data into the second file
            String second_file_string = first_file_string.toUpperCase();
            FileWriter second_fwriter = new FileWriter(open_second, true);
            PrintWriter output_second = new PrintWriter(second_fwriter);
            output_second.println(second_file_string);
            output_second.close();
        }
        first_file_input.close();

        System.out.printf("\nThe contents of %s has been changed to uppercase and stored in %s.\n", file_one, file_two);
        System.out.println();
    }
}

}

...