gpt4 book ai didi

java - 如何从标题为全名缩写的列表生成 .txt 文件

转载 作者:行者123 更新时间:2023-11-30 03:55:18 26 4
gpt4 key购买 nike

这就是我想要实现的目标:

编写一个程序代码,以便当允许用户从给定的员工列表中输入员工姓名时,程序将搜索员工的工资数据,然后生成 .txt 文件形式的报告,其文件名是员工名字的第一个首字母,后面是他们的完整姓氏。

例如,如果为 David Davies 生成报告,则该报告将位于名为 DDavies.txt 的文本文件中。

我已经生成了列表,并且知道如何选择我要查找的记录。我的问题是根据用户选择创建一个文本文件。

即如何根据用户输入“David Davies”作为 1 个字符串来创建文件 DDavies.txt。

由于名称具有不同的长度,这意味着每个字符串长度可能不同,因此我无法仅通过索引来挑选字符(或者我不知道如何)。

由于每个全名都在 1 个字符串中,所以我正在考虑编写一个代码来选择第一个字符,然后在中断(空格)之后选择以下字符串,但是因为它全部在 1 个字符串中并且长度不固定,我不知道如何实现这一点。

Filewriter 没有帮助,因为我必须指定 .txt 扩展名来创建文本文件,所以我不知道如何在不自己输入名称的情况下动态生成文本文件(具有指定的标题)。

我正在考虑将字符串分解为名字和姓氏基础,但这将从根本上改变代码,因为我想要完成的是更大程序的一部分。

请原谅我的长介绍,这是我第一次,所以我希望我足够具体。

下面是代码。 (请注意,该报告不需要向用户显示,我只需要以名字首字母-姓氏格式生成)谢谢大家!

//Report.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JOptionPane;


public class Report {

String firstLine = "", secondLine = "", thirdLine = "";
double hours = 0, wages = 0;
DecimalFormat twoDecimal = new DecimalFormat("0.00");

static ArrayList<String> emps = new ArrayList<String>();

public Report() throws Exception {

//code here the logic to create a report for the user
FileReader file = new FileReader("payroll.txt");
BufferedReader buffer = new BufferedReader(file);

String line;

File check = new File("Overtime.txt");
FileWriter file1;

if (check.exists())
file1 = new FileWriter("Overtime.txt", true);
else
file1 = new FileWriter("Overtime.txt");

int count = 0;
while ((line = buffer.readLine()) != null) {
firstLine = line;
secondLine = buffer.readLine();
thirdLine = buffer.readLine();

double grosspay;
emps.add(line);
}//end while

buffer.close();
file1.close();

String empList = "";

Collections.sort(emps);
for (String str : emps) {
empList += str + "\n";
}

//Employee Listing (names)
JOptionPane.showMessageDialog(null, "Name:\n" + empList, "Employee Listing",
JOptionPane.PLAIN_MESSAGE);


//Get input then of desired employee name to save employee data to a file
String userInput = "";

while (userInput == null || userInput.equals("")) {

userInput = JOptionPane.showInputDialog("To get a payroll report, enter a name from the list");
}

if (empList.toLowerCase().contains(userInput.toLowerCase())) {

/*Upon retrieval of a CORRECT employee name that exists from the employee list,
open payroll.txt file, grab the employee data from the name given
and write the emp's data to a file given the employee’s first initial of his / her first name,
followed by their complete last name. **THIS IS WHERE I NEED HELP!!** */

/**Examples of random names to choose from, we have David Davies, Hyacinth Ho, Betty Boop etc**/


// "Report Generated" Notification
JOptionPane.showMessageDialog(null, "Report Generated.", "Result", JOptionPane.PLAIN_MESSAGE);
}

//Error Message
else {
JOptionPane.showMessageDialog(null, "Error!! Name invalid or doesn't exist, please try again.");
}

System.exit(0);
} //END of Public Report ()

public static void main(String[] args) throws Exception {
new Report();
} //End of Main

} // End of Report Class

最佳答案

经检查用户输入不为空且正确。试试这个:

String userInput; 

....
String filename;
String[] split = userInput.split(" ");

//get the first names first character and gets the last name
filename = userInput.charAt(0)+split[split.length-1];

关于java - 如何从标题为全名缩写的列表生成 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23372674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com