标记上的语法错误、错位的构造 [英] Syntax error on token(s), misplaced construct(s)
本文介绍了标记上的语法错误、错位的构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何修复此错误 ---> 标记上的语法错误、错位的构造错误在下面指示的行中.注意:此代码是在网络上复制的,并试图将其用作学习工具我正在使用 Eclipse谢谢!
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!
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();
}
}
}
推荐答案
您需要将所有语句放在代码块中的声明之后,例如方法而不是类块.从逻辑上讲,将所有语句放在代码块中可能是有意义的,但非声明性语句需要包含在新块中
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();
}
}
这篇关于标记上的语法错误、错位的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文