如何显示多个用户输入的答案以及如何将其全部写入文本文件

问题描述 投票:0回答:1

所以我的代码遇到了另一个问题。我要做的是能够输出用户输入的不同条目。我还希望能够将所有上述条目输出到.txt文件。稍后将用于Microsoft Excel。到目前为止,这是我的代码:

import java.io.FileNotFoundException;
import java.io.PrintWriter;

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

public class Project1{


  public static void main(String[] args) throws FileNotFoundException

{

  String fileName = "out.txt";
  try {


    Scanner keyboard = new Scanner(System.in);
    Scanner user_input = new Scanner(System.in);
    Scanner scan = new Scanner(System.in);

    PrintWriter outFile = new PrintWriter("Project1.out");

    String employee_Fname;
    String employee_Lname;
    String employee_city;
    String employee_state;
    double empzip;
    String employee_job;
    double empsal;
    char again;
    int count = 1;
    String answer;

    do {

        System.out.print("Enter Employees First Name: ");
        employee_Fname = user_input.next();
        System.out.println();

        System.out.print("Enter the employee's last name: ");
        employee_Lname = user_input.next();
        System.out.println();

        System.out.print("Enter employee's city: ");
        employee_city = user_input.next();
        System.out.println();

        System.out.print("Enter employee's state: ");
        employee_state = user_input.next();
        employee_state.toUpperCase();
        System.out.println();

        System.out.print("Enter employee's zipcode: ");
        empzip = keyboard.nextDouble();
        System.out.println();

        System.out.print("Enter employee's job title: ");
        employee_job = user_input.next();
        System.out.println();

        System.out.print("Enter employee's salary: ");
        empsal = keyboard.nextDouble();
        System.out.println();



        while(empsal > 2000000) {
            System.out.println();
            System.out.println("Invalid salary entered! Please tryn again.");

            System.out.print("Enter employee's salary: ");
            empsal = keyboard.nextDouble();
            System.out.println();



            System.out.println();
        }



        System.out.print("Do you want to enter another employee? Y/N?");
        answer = keyboard.next();

    } while (answer.equals("Y"));

    outFile.printf("Employee first name is: %n "+ employee_Fname +"%n");
    outFile.printf("Employee last name is: %n " + employee_Lname +"%n");
    outFile.printf("Employee city is: %n " + employee_city + "%n");
    outFile.printf("Employee state is: %n " + employee_state +"%n");
    outFile.printf("Employee zipcode is: %n " + empzip + "%n");
    outFile.printf("Employee job is: %n  " + employee_job +"%n");
    outFile.printf("Employee salary is:  %n " + empsal +"%n");

    outFile.close();

} catch (FileNotFoundException e) {

  e.printStackTrace();
}
}
}

但是输出什至没有给我一个文本文件,如果我输入了第二个条目,它将删除我以前拥有的条目,并且仅显示最后输入的条目。也许我把“尝试”提早了?

编辑:嘿,没关系,我想通了!原来我没有正确定义“文件名”。但是,我的程序仍然有麻烦。我仍然需要帮助,因为它除了最后输入的条目外不会显示其他任何内容。不仅如此,txt文件为空。我很高兴我至少看到了txt文件,但它是空的。

`

javascript java try-catch user-input jgrasp
1个回答
0
投票

您需要将结果打印到文件中,然后才能转移到下一位员工。另外,您需要使用带有FileWriter的PrintWriter,最后带有文件,以便您可以追加内容,而不必像其他用户指出的那样覆盖内容。

    public static void main(String[] args) {
        String fileName = "out.txt";

        try {
            Scanner keyboard = new Scanner(System.in);

            Scanner user_input = new Scanner(System.in);

            Scanner scan = new Scanner(System.in);

            String employee_Fname;

            String employee_Lname;

            String employee_city;

            String employee_state;

            double empzip;

            String employee_job;

            double empsal;

            char again;

            int count = 1;

            String answer;

            do {
                System.out.print("Enter Employees First Name: ");
                employee_Fname = user_input.next();
                System.out.println();

                System.out.print("Enter the employee's last name: ");
                employee_Lname = user_input.next();
                System.out.println();

                System.out.print("Enter employee's city: ");
                employee_city = user_input.next();
                System.out.println();

                System.out.print("Enter employee's state: ");
                employee_state = user_input.next();
                employee_state.toUpperCase();
                System.out.println();

                System.out.print("Enter employee's zipcode: ");
                empzip = keyboard.nextDouble();
                System.out.println();

                System.out.print("Enter employee's job title: ");
                employee_job = user_input.next();
                System.out.println();

                System.out.print("Enter employee's salary: ");
                empsal = keyboard.nextDouble();
                System.out.println();

                while (empsal > 2000000) {
                    System.out.println();
                    System.out.println("Invalid salary entered! Please tryn again.");

                    System.out.print("Enter employee's salary: ");
                    empsal = keyboard.nextDouble();
                    System.out.println();


                    System.out.println();
                }

                try (PrintWriter outFile = new PrintWriter(new FileWriter(new File("Project1.out"), true))) {
                    outFile.printf("Employee first name is: %n " + employee_Fname + "%n");
                    outFile.printf("Employee last name is: %n " + employee_Lname + "%n");
                    outFile.printf("Employee city is: %n " + employee_city + "%n");
                    outFile.printf("Employee state is: %n " + employee_state + "%n");
                    outFile.printf("Employee zipcode is: %n " + empzip + "%n");
                    outFile.printf("Employee job is: %n  " + employee_job + "%n");
                    outFile.printf("Employee salary is:  %n " + empsal + "%n");
                }

                System.out.print("Do you want to enter another employee? Y/N?");

                answer = keyboard.next();
            } while (answer.equals("Y"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.