如何将方法的返回值作为另一个方法的参数应用?

时间:2018-02-04 01:32:09

标签: java

我一直在研究这个问题,但我似乎无法找到解决这个问题的方法。这里有一些pseduocode来澄清我的问题:

public static void main(String[] args){
    System.out.println("Enter your character: ");
    Scanner in = new Scanner(System.in);
    String inEntry = in.next();
    entryBuilder(inEntry); //this is where entrybuilder takes in an input
public static void findWord(){
     if (validWord(INSERT RETURN FROM METHOD ENTRYBUILDER HERE)){
          //some code
public static String entryBuilder(String input){
    String a = input;
    String prefix = "";
    prefix += a;
    return prefix;
}

我似乎无法理解如何将它拼凑在一起。任何帮助表示赞赏。欢呼声。

3 个答案:

答案 0 :(得分:0)

public static void main(String[] args){
    System.out.println("Enter your character: ");
    Scanner in = new Scanner(System.in);
    String inEntry = in.next();
    entryBuilder(inEntry); //this is where entrybuilder takes in an input
       findWord("you word") //  or findWord(methodThatReturnsString());
    }

public static void findWord(String word){
     if (validWord(INSERT RETURN FROM METHOD ENTRYBUILDER HERE)){
          //some code
}
public static String entryBuilder(String input){
    String a = input;
    String prefix = "";
    prefix += a;
    return prefix;
}
public String methodThatReturnsString(){
      return "some strings";
}

答案 1 :(得分:0)

你需要以某种方式获取findWord中的String值,所以:

public static void findWord(){

    Scanner in = new Scanner(System.in);
    String inEntry = in.next();

     if (validWord(entryBuilder(inEntry))){
          //some code
}

虽然在此方法之外定义扫描仪可能会更好,但请将扫描仪作为参数传递。

答案 2 :(得分:0)

这是你想要做的吗?

    public static void main(String[] args){
        String inEntry = "A test string";
        String entry = entryBuilder(inEntry);
        findWord(entry);
    }

    private static void findWord(String s){
        if (validWord(s)) {
            System.out.println(s + "is valid");
        }
    }

    private static String entryBuilder(String input){       
        String prefix = "";
        prefix += input;
        return prefix;
    }

    private static boolean validWord(String s) {
        return ! s.trim().isEmpty();
    }

(发布mcve是一种很好的做法)