简体   繁体   English

我如何从字符串数组Hashmap调用方法

[英]How do i call a method from a String array Hashmap

I have this block of code, and I'm a bit of a rookie to Java and wonder is it possible to call a void method from an hashmap. 我有这段代码,对Java有点新手,想知道是否可以从哈希图中调用void方法。

eg 例如

HashMap<String, String[]> responses = new HashMap<String, String[]>();
String[] temp5 ={ " Assignment 1", "Assignment 2"};
    responses.put("what is the current assignment", Writer());

the void function is 虚函数是

Void Writer(){

File file = new File("data.txt");


try (BufferedWriter wr = new BufferedWriter(newFileWriter(file.getAbsoluteFile(), true))) {



        System.out.println(" enter what you want to teach me");
        Scanner Keyboard = new Scanner(System.in);
        String lines = Keyboard.nextLine();
        // for(String line : lines){
         wr.write(lines);
        wr.write("\n");

        wr.newLine();
        // }
        // }
        wr.close();
    } catch (IOException e) {
        System.out.println(" cannot write to file " + file.toString());
    }

    return null;

}

Yes, you can call methods inside HashMap put , but that method has to return whatever your HashMap stores. 是的,您可以在HashMap put调用方法,但是该方法必须返回您的HashMap存储的内容。

In your case HashMap<String, String[]> your method must return String[] 在您的情况下, HashMap<String, String[]>您的方法必须返回String[]

Imagine a method 想象一个方法

public String[] fakeData() {
   String[] result;
   //some logic that initializes result
   return result;
}

So you could call it: 所以你可以这样称呼:

responses.put("Foo", fakeData());

A complete example: 一个完整的例子:

public class Test {

    public String[] fakeData() {
        String[] x = new String[5];
        Random rand = new Random();
        for(int i = 0; i < 5; i++) {
            x[i] = "Data: " + rand.nextInt(50);
        }
        return x;
    }

    public static void main(String[] args) throws Exception {
        Test t = new Test();
        HashMap<String,String[]> responses = new HashMap<>();
        responses.put("Foo", t.fakeData());

        for(Map.Entry<String,String[]> entry : responses.entrySet()) {
            System.out.println("Key: " + entry.getKey());
            System.out.println("Value: ");
            for(String s : entry.getValue()) {
                System.out.println("\t" + s);
            }
        }
    }
}

i am creating a void function which writes to a file. 我正在创建一个写入文件的void函数。 So the void function is 所以void函数是

    public static void Writer() {

    File file = new File("data.txt");
    try (BufferedWriter wr = new BufferedWriter(new FileWriter(
            file.getAbsoluteFile(), true))) { // Creates a writer object
                                                // called wr
                                                // file.getabsolutefile
                                                // takes the filename and
                                                // keeps on storing the old
                                                // data

        System.out.println(" enter what you want to teach me");
        Scanner Keyboard = new Scanner(System.in);
        String lines = Keyboard.nextLine();
         wr.write(lines);
        wr.write("\n");

        wr.newLine();

        wr.close();
    } catch (IOException e) {
        System.out.println(" cannot write to file " + file.toString());
    }


}

and the hashmap function is hashmap函数是

HashMap<String, String[]> responses = new HashMap<String, String[]>();
    String[] temp0 = { "What does that suggest to you?", "I see.",
            "I'm not sure I understand you fully.", "Can you elaborate?",
            "That is quite interesting." };
    responses.put("NOTFOUND", temp0);


    String[] temp1 = { "I'm sorry I do not know, but can you help me learn?" };
    responses.put("sure", temp1);

     String[] temp2 = { "I am sorry to hear you are *.",
            "How long have you been *?",
            "Do you believe it is normal to be *?", "Do you enjoy being *?" };
    responses.put("i am", temp2);
    responses.put("i'm", temp2);

    String[] temp3 = { "Tell me more about such feelings.",
            "Do you often feel *?", "Do you enjoy feeling *?",
            "Why do you feel that way?" };
    responses.put("i feel", temp3);

   //String [] temp8 = {Writer()};

    String[] temp4 = {"is that all?"};

    responses.put("now",  temp4);
      responses.put("now",Writer() );

    String[] temp5 ={ " Assignment 1", "Assignment 2"};
    responses.put("what is the current assignment", temp5);

    String[] keywords = { "i think", "i am", "i'm", "i feel","sure","learn","done","now","what is the current assignment" };

So the purpose is to call the Writer method each instance where any keyword is found. 因此,目的是在找到任何关键字的每个实例中调用Writer方法。 In this case the keyword is "now" and the writer function is called to write to the file. 在这种情况下,关键字为“ now”,并调用writer函数以写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM