为什么我在Java中遇到无法访问的语句错误? [英] Why am I getting an Unreachable Statement error in Java?

查看:861
本文介绍了为什么我在Java中遇到无法访问的语句错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译这个程序时,第21行出现无法访问的声明错误: p>

  import java.util。*; 
import java.io. *;
import java.nio.file。*;
import java.lang.StringBuilder;
$ b class FilePrep {
public static void main(String args []){
}
public String getStringFromBuffer(){
try {
Path file = Paths.get(testfile2.txt);
FileInputStream fstream = new FileInputStream(testfile2.txt);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String inputLine = null;
StringBuffer theText = new StringBuffer(); ((inputLine = br.readLine())!= null){
theText.append(inputLine +);

while

return theText.toString();
System.out.println(theText); //< - line 21
}
catch(Exception e)
{
System.err.println(Error:+ e.getMessage());
返回null;





完整的编译器输出是:

  Main.java:21:error:unreachable statement 
System.out.println(theText);
^
Main.java:28:错误:缺少返回语句
}
^
2错误

我认为 return 语句处于正确的位置......他们似乎至少对我来说,这个程序看起来很简单,而我从中克隆出来的程序却很简单,我很难搞清楚为什么这个语句是无法访问的。

复制代码时我做了什么错误,我该如何纠正它?

解决方案

是正确的,假设你的问题在这里:

  return theText.toString(); 
System.out.println(theText);

return 函数将< 您的方法,意味着将不会执行任何代码。
如果你想让你的印刷品通过,你应该把它移到return语句的上面。


When I try to compile this program, I get an "unreachable statement" error in line 21:

import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.lang.StringBuilder;

class FilePrep {
    public static void main(String args[]) {
    }
    public String getStringFromBuffer() {
        try {
            Path file = Paths.get("testfile2.txt");
            FileInputStream fstream = new FileInputStream("testfile2.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));  
                String inputLine = null;                    
            StringBuffer theText = new StringBuffer();  

            while((inputLine=br.readLine())!=null) {
                theText.append(inputLine+" ");
            }
            return theText.toString();
            System.out.println(theText); // <-- line 21
        }
        catch (Exception e)
        {
            System.err.println("Error: " + e.getMessage());
            return null;
        }
    }
}

The full compiler output is:

Main.java:21: error: unreachable statement
            System.out.println(theText);
            ^
Main.java:28: error: missing return statement
    }
    ^
2 errors

I think the return statements are in the right places...they seem to be to me at least, and the program seems so simple compared to the one that I cloned it from, that I'm having a really hard time figuring out why this statement is unreachable.

What did I do wrong while copying the code, and how do I need to correct it?

解决方案

You were right assuming that your problem is here:

return theText.toString();
System.out.println(theText);

the return function will terminate your method, meaning no line of code past it will be executed. If you want your print to go through, you should move it above the return statement.

这篇关于为什么我在Java中遇到无法访问的语句错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆