我有一个system.out.print错误,我不知道为什么?

时间:2013-11-27 12:48:35

标签: java

这是我的代码:

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并且在程序中表现出色,但我认为这不会导致问题。

7 个答案:

答案 0 :(得分:6)

你不能使用

 System.out.print(val);

一边是方法。您应该将System.out.print(val);放在方法中。

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

答案 1 :(得分:4)

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

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

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

答案 2 :(得分:2)

System.out.print(val);

在方法范围之外。

答案 3 :(得分:2)

System.out.print(val);

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

答案 4 :(得分:2)

以这种方式定义类:

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

你不能在那里执行语句。它们应位于方法中。请参阅Declaring Classes

答案 5 :(得分:1)

你无法调用

System.out.print(val);

在课堂上。如果要执行某些代码,则需要将其放在

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

答案 6 :(得分:0)

写下你的陈述

  System.out.print(val);

在函数或主函数中。