Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从标题为全名缩写的列表生成.txt文件_Java_Arrays_String_Arraylist_Filewriter - Fatal编程技术网

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

Java 如何从标题为全名缩写的列表生成.txt文件,java,arrays,string,arraylist,filewriter,Java,Arrays,String,Arraylist,Filewriter,这就是我试图实现的目标: 编写程序代码,以便当允许用户输入给定员工列表中的员工姓名时,程序将搜索 然后以.txt文件的形式生成报告, 其文件名是员工姓名的首字母,后跟 他们的全名 例如,如果为David Davies生成报告,则该报告将位于名为DDavies.txt的文本文件中 我已经生成了列表,我知道如何选择我要查找的记录。我的问题是根据用户选择创建文本文件 i、 e如何根据用户输入的“David Davies”作为字符串创建文件DDavies.txt 由于名称具有不同的长度,这意味着每个字符

这就是我试图实现的目标:

编写程序代码,以便当允许用户输入给定员工列表中的员工姓名时,程序将搜索 然后以.txt文件的形式生成报告, 其文件名是员工姓名的首字母,后跟 他们的全名

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

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

i、 e如何根据用户输入的“David Davies”作为字符串创建文件DDavies.txt

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

因为每个全名都在一个字符串中,所以我想写一个代码来选择第一个字符,然后在空格后选择下一个字符串,但是因为它都在一个字符串中,并且长度不是固定的,所以我不知道如何做到这一点

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
//Report.java
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.text.DecimalFormat;
导入java.util.ArrayList;
导入java.util.Collections;
导入javax.swing.JOptionPane;
公开课报告{
字符串firstLine=“”、secondLine=“”、thirdLine=“”;
双倍工时=0,工资=0;
DecimalFormat twoDecimal=新的DecimalFormat(“0.00”);
静态ArrayList emps=新ArrayList();
公共报表()引发异常{
//在此处编码为用户创建报告的逻辑
FileReader文件=新的FileReader(“payroll.txt”);
BufferedReader buffer=新的BufferedReader(文件);
弦线;
文件检查=新文件(“timeout.txt”);
文件编写器文件1;
if(检查.exists())
file1=新的FileWriter(“超时.txt”,true);
其他的
file1=新的FileWriter(“加班时间.txt”);
整数计数=0;
而((line=buffer.readLine())!=null){
第一行=第二行;
secondLine=buffer.readLine();
thirdLine=buffer.readLine();
重瓣罗缎;
皇帝添加(行);
}//结束时
buffer.close();
file1.close();
字符串empList=“”;
收集.分类(emps);
用于(字符串str:emps){
empList+=str+“\n”;
}
//员工名单(姓名)
JOptionPane.showMessageDialog(空,“名称:\n”+员工列表,“员工列表”,
JOptionPane.普通消息);
//然后获取所需员工姓名的输入,以将员工数据保存到文件中
字符串userInput=“”;
while(userInput==null | | userInput.equals(“”){
userInput=JOptionPane.showInputDialog(“要获取工资单报告,请从列表中输入名称”);
}
if(empList.toLowerCase().contains(userInput.toLowerCase())){
/*从员工列表中检索到正确的员工姓名后,
打开payroll.txt文件,从给定的名称中获取员工数据
并将emp数据写入一个文件,给出员工姓名的首字母,
然后是他们的全名。**这是我需要帮助的地方!!***/
/**可供选择的随机名称示例有David Davies、Hyacinth Ho、Betty Boop等**/
//“生成报告”通知
showMessageDialog(null,“生成的报告”,“结果”,JOptionPane.PLAIN\u消息);
}
//错误消息
否则{
showMessageDialog(null,“错误!!名称无效或不存在,请重试”);
}
系统出口(0);
}//公共报告()
公共静态void main(字符串[]args)引发异常{
新报告();
}//主管道末端
}//报告类结束

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

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