我正在尝试从调用方法获取返回值savedName。 我需要在另一个方法中使用字符串保存名称中的值
if (n == 0){
String fullName = readFromFile(); //wont let me do this
text.setText("Welcome " + fullName + ".");
}
private String readFromFile(String savedName){
savedName="";
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
savedName = br.readLine();
System.out.println(savedName);
} catch (IOException e) {
e.printStackTrace();
}
return savedName;
}
答案 0 :(得分:1)
您尝试调用的方法必须具有匹配的方法签名。在这种情况下,数字和类型参数必须相同。最直接的方法是从方法中删除String参数,并在方法中创建一个名为savedName的String变量并将其返回,如下所示:
private String readFromFile(){
String savedName="";
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
savedName = br.readLine();
System.out.println(savedName);
} catch (IOException e) {
e.printStackTrace();
}
return savedName;
}
答案 1 :(得分:0)
我在String fullName = readFromFile("pass some String here"); //wont let me do this
if (n == 0){
String fullName = readFromFile("pass some String here"); //now this will let you do
text.setText("Welcome " + fullName + ".");
}
private String readFromFile(String savedName){
savedName="";
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
savedName = br.readLine();
System.out.println(savedName);
} catch (IOException e) {
e.printStackTrace();
}
return savedName;
}