我有这段代码,而且我是Java的新手,并且想知道是否可以从hashmap调用void方法。
例如
HashMap<String, String[]> responses = new HashMap<String, String[]>();
String[] temp5 ={ " Assignment 1", "Assignment 2"};
responses.put("what is the current assignment", Writer());
void函数是
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;
}
答案 0 :(得分:0)
是的,您可以在HashMap put
中调用方法,但该方法必须返回HashMap
存储的任何内容。
根据您的情况HashMap<String, String[]>
,您的方法必须返回String[]
想象一个方法
public String[] fakeData() {
String[] result;
//some logic that initializes result
return result;
}
所以你可以称之为:
responses.put("Foo", fakeData());
一个完整的例子:
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);
}
}
}
}
答案 1 :(得分:0)
我正在创建一个写入文件的void函数。所以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());
}
}
并且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" };
因此,目的是为每个找到任何关键字的实例调用Writer方法。在这种情况下,关键字是“now”,并且调用writer函数来写入文件。