Java 标记上的语法错误、错位的构造

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21589784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:32:27  来源:igfitidea点击:

Syntax error on token(s), misplaced construct(s)

javaeclipseexcelsyntaxapache-poi

提问by user3277243

How to fix this error ---> Syntax error on token(s), misplaced construct(s) The error is at the line below indicated. Note: This code was copied on the web and trying to get it to work as a learning tool I'm using Eclipse Thanks!

如何修复此错误 ---> 标记上的语法错误、错位的构造 错误在下面指示的行中。注意:此代码是在网络上复制的,并试图将其用作学习工具,我正在使用 Eclipse 谢谢!

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class EcellTest22 {
     //Blank workbook
    XSSFWorkbook workbook = new XSSFWorkbook();

    //Create a blank sheet
    XSSFSheet sheet = workbook.createSheet("Employee Data");

    //This data needs to be written (Object[])
    Map<String, Object[]> data = new TreeMap<String, Object[]>();
     //
    data.put("1", new Object[]{"ID","NAME", "LASTNAME"}); <--Syntax error on token(s), misplaced construct(s) 


    data.put("2", new Object[]{1, "Amit", "Shukla"});
    data.put("3", new Object[]{2, "Lokesh", "Gupta"});
    data.put("4", new Object[]{3, "John", "Adwards"});
    data.put("5", new Object[]{4, "Brian", "Schultz"});

    //Iterate over data and write to sheet
    Set<String> keyset = data.keySet();

    int rownum = 0;
    for (String key : keyset) 
    {
        //create a row of excelsheet
        Row row = sheet.createRow(rownum++);

        //get object array of prerticuler key
        Object[] objArr = data.get(key);

        int cellnum = 0;

        for (Object obj : objArr) 
        {
            Cell cell = row.createCell(cellnum++);
            if (obj instanceof String) 
            {
                cell.setCellValue((String) obj);
            }
            else if (obj instanceof Integer) 
            {
                cell.setCellValue((Integer) obj);
            }
        }
    }
    try 
    {
        //Write the workbook in file system
        FileOutputStream out = new FileOutputStream(new File("C:\Documents and Settings\admin\Desktop\imp data\howtodoinjava_demo.xlsx"));
        workbook.write(out);
        out.close();
        System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
    } 
    catch (Exception e)
    {
        e.printStackTrace();
    }

    }

}

回答by Reimeus

You need to place all statements after the declarations in a code block, e.g. method rather than the class block. Logically it probably makes sense to place all statements in the code block but the non-declarative statements need to be enclosed within the new block

您需要将所有语句放在代码块中的声明之后,例如方法而不是类块。从逻辑上讲,将所有语句放在代码块中可能是有意义的,但非声明性语句需要包含在新块中

private void processFile() {
    data.put("1", new Object[]{"ID","NAME", "LASTNAME"}); // <--Syntax error  
     ...//snip
  } catch (Exception e) {
     e.printStackTrace();
  } 
}

回答by Kevin Bowersox

Place all of your code within a main method:

将所有代码放在一个 main 方法中:

public static void main(String[] args) {
        //All of your code goes here

}

Statements (this does not include declarations) must be executed inside a block. It appears that you are conducting a test of some code and that this is not meant to be an actual object in your code, so you must place it within the main method.

语句(这不包括声明)必须在块内执行。看起来您正在对某些代码进行测试,并且这并不是您代码中的实际对象,因此您必须将其放置在 main 方法中。

回答by user3277243

Issue resolved. I created a new project in Eclipse, added the POI (jar) to the libraries and the syntax error is no longer displayed.

问题解决了。我在 Eclipse 中创建了一个新项目,将 POI(jar)添加到库中,并且不再显示语法错误。

回答by Ved Prakash

Paste try and catch block inside any method. Above question your try block only inside the class that's why your getting Syntax Error messageor Learn deeply about try

在任何方法中粘贴 try 和 catch 块。上面的问题是你的 try 块只在课堂内,这就是为什么你收到语法错误消息或深入了解 try

public void yourMethod(){    
        try 
         {
           //Write the workbook in file system
           FileOutputStream out = new FileOutputStream(new File("C:\Documents and Settings\admin\Desktop\imp data\howtodoinjava_demo.xlsx"));
                    workbook.write(out);
                    out.close();
                    System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
                } 
                catch (Exception e)
                {
                    e.printStackTrace();
            }
        }