9

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();
    }

    }

}
rgettman
  • 176,041
  • 30
  • 275
  • 357
user3277243
  • 129
  • 1
  • 1
  • 5

4 Answers4

8

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();
  } 
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • 2
    This includes import statements. – Noumenon Nov 26 '17 at 17:51
  • 2
    Obviously this doesnt include import statements :) – Reimeus Nov 26 '17 at 19:58
  • 3
    I meant to say that if an import statement gets down in between your braces, it will cause this error just like other code. Happened to me with a velocity Include macro. – Noumenon Nov 28 '17 at 05:02
  • Noumenon, that was the fix for my issue too. Someone had created 2 classes on the same page, and put one import statement for the 2nd class lower on the page after the first class, not in any code black (because imports wouldn't be in one). When I moved it back to the top, the error went away. I guess they can only be at the top. – Azurespot Feb 04 '20 at 21:46
3

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

user3277243
  • 129
  • 1
  • 1
  • 5
2

Place all of your code within a main method:

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.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0

Paste try and catch block inside any method. Above question your try block only inside the class that's why your getting Syntax Error message or Learn deeply about 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();
            }
        }
Onic Team
  • 1,620
  • 5
  • 26
  • 37