Java:如何使用filewriter类将方法中的族名称附加到外部文件? [英] Java: how do I append the names of a family in a method to an external file using the filewriter class?

查看:65
本文介绍了Java:如何使用filewriter类将方法中的族名称附加到外部文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  static String fileName= "HW13_DataFile.txt";

    public static void main(String[] args) {
        // TODO code application logic here
        
        String name;          // Family's last name 
        String family[];      // array that will contain all family members
        
                    // Ask for the family name
            name = JOptionPane.showInputDialog(null, 
                    "What is the name of the family?");
            
            family = getFamily(name);
            
            writeData(family);  
            
 
    }
    
   
    public static String [] getFamily(String name) {
       String ans;       // answer. Stores string.
       int count;        // Converts 'ans' string to integer.
       String first;     // Stores each family member first name. 
       int cont;
      
       
       // input dialog
       ans = JOptionPane.showInputDialog(null, "How many members in the " + name +
                " Family?");
       
       // converts users input into a number
       count = Integer.parseInt(ans);
       
       // string array stores the number of family members 
       String[] family = new String[count];
       
       // for loop 
       for (int num = 0; num < family.length; num++) {
           
        // input dialog
        first = JOptionPane.showInputDialog(null, "Enter first name: " 
                + (num + 1));
        
        // array stores first and last name of each family member
        family[num] = first + " " + name;
        
       }
       cont = JOptionPane.showConfirmDialog(null,
                    "Do you want to add another name?", 
                    "Enter Names", JOptionPane.YES_NO_OPTION);
        
        while (cont == JOptionPane.YES_OPTION);
        
       return family;
    }
    
    private static void writeData(String family) throws IOException {
        
        try (FileWriter fw = new FileWriter(fileName, true)) {
            fw.append(family);
            fw.close();
        }
    }
}

writeData - Append the names of one family to an external file, with each family using one line.
Use the FileWriter class.





我尝试过:



我已经尝试了我编写的代码,但我知道它不正确。



What I have tried:

I've tried what's on the code I've written but I know its incorrect.

推荐答案

那是因为你的功能他们的回报类型不匹配。看看你的功能,

That is because your functions and their return types are having a mismatch. Have a look at your functions,
public static String[] getFamily(String name)

public static void writeData(String family)



我知道您想要捕获姓氏,然后逐个写入 - 或者一起写。但是,您正试图保存它。但问题是您的参数类型是 String ,并且您传递的参数是 String []




I understand that you want to capture the family names and then write them one by one — or together. However, you are trying to save it. But the problem is your parameter type is String, and the arguments that you are passing are String[].

family = getFamily(name); // String[]
writeData(family);        // accepts String



这是问题是,你不能指望驴子取代马匹,因此,你需要确保只有马匹才能通过这项功能。有几种方法可以做到这一点,一种方法是更改​​功能签名,


This is the problem, you cannot expect donkeys to take place of horses and thus, you need to make sure only horses get passed to the function. There are several ways of doing this, one way is to change the function signature,

public static void writeData(String[] familyNames)



然后,添加一个循环来为每个族写入数据。否则更简单的方法是迭代getFamily的结果并逐个保存它们的名称,这将更简单,就像这样,


Then, add a loop to write the data for each of the family. Otherwise a much simpler way is to iterate over the result of getFamily and save them names one by one, that will be simpler, like this,

family = getFamily(name);

for (String name : family) {
    writeData(name); // String is passed
}



这样,您的代码将是相同的,数据类型将匹配。


This way, your code will be same, and the data types will be matched.


这篇关于Java:如何使用filewriter类将方法中的族名称附加到外部文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆