Why am I getting an Unreachable Statement error in Java?

 1 import java.util.*;
 2 import java.io.*;
 3 import java.nio.file.*;
 4 import java.lang.StringBuilder;
 5
 6 class FilePrep {
 7     public static void main(String args[]) {
 8     }
 9     public String getStringFromBuffer() {
10         try {
11             Path file = Paths.get("testfile2.txt");
12             FileInputStream fstream = new FileInputStream("testfile2.txt");
13             BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
14                 String inputLine = null;
15             StringBuffer theText = new StringBuffer();
16
17             while((inputLine=br.readLine())!=null) {
18                 theText.append(inputLine+" ");
19             }
20             return theText.toString();
21             System.out.println(theText); // <-- line 21
22         }
23         catch (Exception e)
24         {
25             System.err.println("Error: " + e.getMessage());
26             return null;
27         }
28     }
29 }

The full compiler output is:

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

解答

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.

reference: http://stackoverflow.com/questions/11488988/why-am-i-getting-an-unreachable-statement-error-in-java

时间: 2024-10-06 15:40:24

Why am I getting an Unreachable Statement error in Java?的相关文章

unreachable statement的解决方法,其实就是作用域问题

unreachable statement翻译过来,也就是无法访问声明.这不是什么大问题,是作用域问题. 我下面的问题代码就是这样: @Override public int getItemViewType(int position) { return super.getItemViewType(position);//已经return,所以下面语句根本无法执行,作用域截止 Fruit fruit = getItem(position);//错误,提示unreachable statement

unreachable statement

今天突然出现一警告,unreachable statement . 上网查查,发现有java同仁遇过同样的问题, 明白了,对应到我的项目就是, 在die()后已经不会继续执行了.没必要return 0: stackoverflow真是赞.

记录实时问题:java 出现unreachable statement异常

java编译器把unreachable statement标记为运行时错误,一个unreachable statement就是编译器决定永远不会执行它.下面的几种情况会出现 unreachable statement:(1)在reutrn语句后写语句.(2)在throw语句后写语句.(3)break.continue语句之后定义语句.(4)“\u10100”//合法,相当于‘\u1010’和字符串“0”.(5)移位运算符可以用于long int char short byte.(6)类的访问控制

真机测试报错ERROR/AndroidRuntime: java.lang.RuntimeException: setParameters failed解决办法

这个错误是和调用相机摄像头相关的. 产生这个错误的原因主要在于代码控制分辨率的显示和真机测试分辨率不一样. 一:解决办法 WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Camera.Parameters parameters = camera.getParameters();// 得到摄像头的参数 para

Error: Unsupported java version &#39;1.8&#39; in line 2 of file &#39;proguard.conf&#39;

执行Maven Install打包的时候,出现以下错误信息: [proguard] Error: Unsupported java version '1.8' in line 2 of file 'F:\Workspaces\pro-test\proguard.conf', [proguard] included from argument number 6 这是因为在proguard.conf文件中显示的指定的Java的版本,但是net\sf\proguard\proguard\4.4\pro

Fatal error compiling: java.lang.NoSuc hFieldError??

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project easemob-core: Fatal error compiling: java.lang.NoSuchFieldError: pid -> [Help 1][ERROR][ERROR] To see the full stack trace of the e

Inceptor [Code: 40000, SQL State: 42000] COMPILE FAILED: Internal error NullPointerException: [Error 40000] java.lang.NullPointerException

下面代码报空指针 with `__all_dim__` as ( select * from ( select from_unixtime(unix_timestamp(`__bts__`) -1,'yyyy-MM-dd HH:mm:ss') as `__bts__` from ( select concat_ws(' ', `d`.`date`, `t`.`time_of_day`) as `__bts__` from `ecmp`.`dim_date` as `d` left join `e

java.lang.NoSuchMethodException: org.apache.ibatis.executor.statement.StatementHandler.prepare(java.sql.Connection)

此错误是由于版本造成的,如果使用mybatis3.4版本以上,配置拦截器规则应增加Intger @Intercepts({ @Signature( type= StatementHandler.class, method = "prepare", args = {Connection.class,Integer.class} ) })

Maven构建项目后项目报Error错误Java compiler level does not match the version of the installed Java project fac

项目->右键->Project Facets->修改facets中Java版本(下拉箭头出)为要用的版本 Maven构建项目需注意 1.项目右键->Preferences->buildpath->jdk 2.项目右键->Preferences->javacompiler->jdk版本 3.项目右键->Preferences->ProjectFacets->jdk版本 如图所示: