Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在循环内返回_Java - Fatal编程技术网

Java 如何在循环内返回

Java 如何在循环内返回,java,Java,我有一个程序,我需要从一个方法得到一个返回值,问题是他总是返回第一个位置,什么也没有。这是我希望接收输入的方法 public void getCountByArea() { //always receive the same string String inputToValidate = getInputsTxtFromConsole(); String inputToCompare = getInput

我有一个程序,我需要从一个方法得到一个返回值,问题是他总是返回第一个位置,什么也没有。这是我希望接收输入的方法

  public void getCountByArea() {


              //always receive the same string
              String inputToValidate = getInputsTxtFromConsole();
              String inputToCompare = getInputsTxtFromConsole();


}
这是我的另一种方法

public String getInputsTxtFromConsole() {

//read inputs file
    try {

        Scanner scanner = new Scanner(inputFile);

        //read the file line by line
        int lineNum = 0;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            lineNum++;

            return line; //this always returns the same number

        }
    } catch (FileNotFoundException e) {


    }


  return "";

}

不能使用return语句获取多个值,因为它会结束函数调用。最好的方法是堆叠值,然后返回。这是一个使用arraylist的示例

public List getInputsTxtFromConsole() {

//read inputs file
    try {
        List<String> lines = new ArrayList(); //list instance
        Scanner scanner = new Scanner(inputFile);

        //read the file line by line
        int lineNum = 0;
        while (scanner.hasNextLine()) {
            lines.add(scanner.nextLine()); //adding elements
            lineNum++;
        }

    return lines; //then return
    } catch (FileNotFoundException e) {


    }


  return null;

}
public List getInputsTxtFromConsole(){
//读取输入文件
试一试{
列表行=新建ArrayList();//列表实例
扫描仪=新扫描仪(输入文件);
//逐行读取文件
int lineNum=0;
while(scanner.hasNextLine()){
lines.add(scanner.nextLine());//添加元素
lineNum++;
}
返回行;//然后返回
}catch(filenotfounde异常){
}
返回null;
}
第二次编辑

如果要获取每个值,需要循环返回的列表

  public void getCountByArea() {


    //always receive the same string
    List<String> inputToValidate = getInputsTxtFromConsole();
    List<String> inputToCompare = getInputsTxtFromConsole();


    for(String input1 : inputToValidate){ //input1 is a temp variable
        // your custom implementation
        ...
    }
    //another for each
    ...

}
public void getCountByArea(){
//始终接收相同的字符串
List InputOvalidate=getInputsTxtFromConsole();
List inputToCompare=getInputsTxtFromConsole();
对于(字符串input1:InputOvalidate){//input1是一个临时变量
//您的自定义实现
...
}
//每人一份
...
}

您希望发生什么?您是否了解
return
的功能?它的目的是结束函数并返回一个值。如果你不想结束函数执行,你不应该在那里使用
return
。你想在这里做什么?通过从循环内部返回,您完全不需要循环。您的意思是将行添加到列表或其他内容中,然后返回列表吗?您需要澄清您试图执行的操作。您只能使用return语句返回一个值,您需要堆叠所有值​​然后返回我建议你离开电脑,用文字写下你想采取的解决问题的步骤。英语(或您使用的任何语言)描述将建议如何构造Java程序以正确执行任务。是的,在我的getCountByArea中,如何逐个获取输入?请注意,该方法中的那些字符串位于while循环中(我的代码中未显示)