如何顯示多個用戶輸入的答案以及如何將其全部寫入文本文件 (How to display several user inputted answers and how to write it all to a text file)


問題描述

如何顯示多個用戶輸入的答案以及如何將其全部寫入文本文件 (How to display several user inputted answers and how to write it all to a text file)

所以我的代碼遇到了另一個問題。我想要做的是能夠輸出用戶輸入的不同條目。我還希望能夠將所有所述條目輸出到 .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 文件,但它是空的。

`


參考解法

方法 1:

You need to print the results to the file before you move onto the next employee. Also, you need to use a PrintWriter that takes a FileWriter, and ultimately a File so that you can append instead of overwritting the contents as another user noted.

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

(by CarryDoveJason)

參考文件

  1. How to display several user inputted answers and how to write it all to a text file (CC BY‑SA 2.5/3.0/4.0)

#java #user-input #javascript #jgrasp #try-catch






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論