2013-11-27 139 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); 

無論是在功能或主要功能。

相關問題