简体   繁体   中英

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.

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.

In your case HashMap<String, String[]> your method must return 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. So the void function is

    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<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. In this case the keyword is "now" and the writer function is called to write to the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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