在Java中,使用其他方法中的参数调用方法
我脑子里有一个可能的解决方案,但我不太知道如何用代码来解决这个问题。我在Java的方法中调用方法时遇到了麻烦 我有以下代码:在Java中,使用其他方法中的参数调用方法,java,Java,我脑子里有一个可能的解决方案,但我不太知道如何用代码来解决这个问题。我在Java的方法中调用方法时遇到了麻烦 我有以下代码: public Student getStudent(String queryWord){ //the queryWord here should actually be the String result that returnQueryColumn returns } private static Map<String, String> retu
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;
}
我知道这个代码不起作用,但我需要一些帮助,我如何才能实现我的想法?我被困在这个问题上-我如何在其他方法中调用一个方法,并使第一个方法中返回的字符串成为第二个方法中的参数。假设您有学生类:
如果我理解你的意图是正确的,主类可以是这样的
示例代码打印学生全名属性
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);
}
}
调用方法并传递参数?你还不明白什么?我仍然不知道你到底想要实现什么,但你说你有想法,但你不知道如何做到这一点——这在我看来是一个挑战。。。你的投入是什么,你的预期产出是什么?
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);
}
}