2013-11-27 187 views
-1

这是我的代码:我有一个system.out.print错误,我不知道为什么?

public class Outline { 

     int rowIndex=62;//hsample number 
     int colIndex=2;//sample number, will be determined by RNG when I'm done 

     String val=read1(rowIndex,colIndex); 

     System.out.print(val);//This is where the error is, I don't know what's wrong with it. 



    public static final String FILE_NAME = "Copy_of_Words.xls"; 

    public static String read1(int rowIndex, int colIndex){ 

     String value = new String(); 
     HSSFWorkbook wb = null; 


     try { 
      wb= new HSSFWorkbook(new FileInputStream(FILE_NAME)); 
     } catch (Exception e){//in here, say what needs to be done 

     } 

     HSSFSheet sheet=wb.getSheet("SAT"); //here, user input will determine sheet 
     HSSFRow row=sheet.getRow(rowIndex-1); 
     HSSFCell cell=row.getCell(colIndex-1); 

     DataFormatter formatter = new DataFormatter(); 
     value = formatter.formatCellValue(cell); 

     return value; 
    }  
} 

到底是什么是System.out.print用(VAL)的问题?我无法弄清楚。我在程序中使用apache和excel,但我不认为这会导致问题。

+2

错误信息究竟是什么? –

+0

你有什么错误?它是编译时间还是运行时间?如果运行时添加异常堆栈跟踪 –

+1

*永远不会*在一个问题“我有一个错误”,而不会说出那个错误是什么。请参阅http://tinyurl.com/so-list –

回答

6

不能使用

System.out.print(val); 

输出侧的方法。你应该把System.out.print(val);放在一个方法里面。

public void myMethod(){ 
    System.out.print(val); 
} 
4

您尝试在类体中执行语句。你不能那样做。

每个语句都必须在方法(或构造函数或初始化程序块)中。

只有声明(method/field/constructor/...)可以直接在类体中。

+0

提及初始化程序块。 – Axel

1

你不能调用

System.out.print(val); 

上一流水平。如果你想执行一些代码,那么你需要把它放在

  • 方法(如public static void main(String[] args){...}
  • 构造,
  • 或初始化块
2

System.out.print(val); 

不在方法范围之内。

2
System.out.print(val); 

此声明必须存在于某些功能中。

2

类是这样定义的:

class MyClass { 
    // field, constructor, and 
    // method declarations 
} 

不能执行语句那里。他们应该位于方法内。见Declaring Classes

0

写乌尔声明

System.out.print(val); 

无论是在功能或主要功能。

相关问题