这就是我想要实现的目标:
编写程序代码,以便当用户被允许从给定的员工列表中输入员工姓名时,程序将搜索员工的工资单数据,然后以.txt文件的形式生成报告,其文件名是员工名字的第一个首字母,后跟他们的完整姓氏。
例如,如果为David Davies生成了一个报告,则该报告将位于名为DDavies.txt的文本文件中。
我已经生成了列表,我知道如何挑选我要找的记录。我的问题是基于用户选择创建文本文件。
即,如何根据用户输入"David Davies"作为1个字符串创建文件DDavies.txt。
由于名称的长度不同,这意味着每个字符串的长度可能不同,所以我无法单独通过索引来挑选字符(或者我不知道如何挑选(。
由于每个全名都在一个字符串中,我想写一个代码来选择第一个字符,然后在break(空格(后选择下一个字符串,但由于所有字符都在1个字符串中并且长度不固定,我不知道如何实现这一点。
Filewriter也于事无补,因为我必须指定.txt扩展名才能创建文本文件,所以我不知道如何在不输入名称的情况下动态生成文本文件(具有指定的标题(。
我想把字符串分成名字和姓氏,但这将从根本上改变代码,因为我试图完成的是一个更大程序的一部分。
请原谅我的长介绍,这是我第一次,所以我希望我足够具体。
下面是代码。(请注意,报告不需要向用户显示,我只需要以firstInitial LastName格式生成它(谢谢大家!
//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];
我想写一个代码来选择第一个字符,然后选择中断(空格(后的下一个字符串,但由于它都在一个字符串中,并且长度不固定,我不知道如何实现这一点。
- 您可以使用
yourString.charAt(0)
来拾取字符串的第一个字符 - 若要在第一个空间后面拾取字符串,只需使用后面的
yourString.indexOf(' ')
和substring
即可找到第一个空间的索引
示例
String someString = "Foo Bar";
System.out.println(someString.charAt(0)
+ someString.substring(someString.indexOf(' ') + 1))
//+1 because we don't want to include space in substring
输出:FBar
您也可以将".txt"
添加到结果中。
也许这就是您想要的:
String name = "Doctor Who";
String[] name_parts = name.split(" ");
String filename = name_parts[0].charAt(0) + name_parts[1] + ".txt");
//filename = DWho.txt