我脑子里有个解决问题的办法,但是我不太知道如何用代码来解决。我陷入了在Java方法中调用方法的麻烦。
我有此代码:
public Student getStudent(String queryWord){
//the queryWord here should actually be the String result that returnQueryColumn returns
}
private static Map<String, String> returnMap(String myQuery){
String[] params = myQuery.split("=");
Map<String, String> myMap = new HashMap<String, String>();
String myKey = params[0];
String myValue = params[1];
//myKey should be for example firstName, myValue should be the ACTUAL first name of the student
myMap.put(myKey,myValue);
return myMap;
}
private static String returnQueryColumn(Map<String, String> myMap){
//here I want to get just the KEY from the myMap(from the returnMap method)
//that key should be the column in my database, so I need this so that later on I can compare if the given key (firstName) is present in the database as a column
String queryWord = returnMap().get(); //query should get firstName in my case
return queryWord;
}
我知道这段代码行不通,但是我需要一些帮助,我如何才能实现自己的想法?我陷入了困境-我如何才能在其他方法中调用一个方法,并使第一个方法中返回的字符串成为第二个方法中的参数。
答案 0 :(得分:1)
假设您有Student
个课程:
public class Student {
String fullName;
public Student(String fullName) {
this.fullName = fullName;
}
}
如果我正确理解了您的意图,Main
班级可能看起来像这样。
示例代码显示学生的fullName属性。
public class Main {
public static void main(String[] args) {
Student student = getStudent("john=John Appleseed");
System.out.println(student.fullName);
}
public static Student getStudent(String myQuery) {
return returnQueryColumn(myQuery);
}
private static Map<String, Student> returnMap(String myQuery){
String[] params = myQuery.split("=");
Map<String, Student> myMap = new HashMap<String, Student>();
String myKey = params[0];
String myValue = params[1];
Student student = new Student(myValue);
myMap.put(myKey, student);
return myMap;
}
private static Student returnQueryColumn(String myQuery) {
String[] params = myQuery.split("=");
String key = params[0];
return returnMap(myQuery).get(key);
}
}