简体   繁体   中英

How to call a method with parameters?

I'm trying to get the return value savedName from calling the method. I need to use the value from the string saved name in another method

    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;

}

The method you are trying to call must have a matching method signature. In this case the number and type arguments must be the same. The most direct approach is to remove the String argument from your method, and create a String variable called savedName in your method and return it, like this:

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;

}

i have made new changes in String fullName = readFromFile("pass some String here"); //wont let me do this 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;

}

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