Cornel Cornel - 2 months ago 7
Java Question

Strange Behaviour in Eclipse 3.8.1

I'm a newbie here. I have a simple problem in ONE java source file: the row

System.out.pritln(...)
has been treated as an erroneous expression. Here's the code snippet:

package vk.gui;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Properties;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BarcodeEAN;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


public class MatrixSheet1 {
Properties p;
File file;
Document document;
PdfWriter writer;
Image logo = null;
Image EANimg = null;
float mnoz = new Double(72/25.6).floatValue();

int IMG_WIDTH= new Double(35*mnoz).intValue();
int IMG_HEIGHT=new Double(35*mnoz).intValue();
String err=p.getProperty("cell.height");
System.out.println("Arrgh!"); ///-------------->ERROR!
float cell_Height = Float.parseFloat(p.getProperty("cell.height"))*mnoz;
float cell_Width = Float.parseFloat(p.getProperty("cell.width"))*mnoz;


The reported error is


Multiple markers at this line


  • Syntax error on token ""Arrgh!"", delete this token

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




The sout and sysout shortcuts do not work neither. In other existing source files of same package everything is OK, the shortcuts work and the expression does not trigger an error.
I tried to create another source file and copy/paste the content, but I got the same error. What and where went wrong?
I need the printing just for debugging, but this is a bit annoying symptom.
Thanks in advance.

Answer

This happens because you can use System.out.println() only inside methodes. If you would do something like this, it would work:

public class MatrixSheet1 {
    Properties p;
    File file;
    Document document;
    PdfWriter writer;
    Image logo = null;
    Image EANimg = null;
    float mnoz = new Double(72/25.6).floatValue();

    int IMG_WIDTH= new Double(35*mnoz).intValue(); 
    int IMG_HEIGHT=new Double(35*mnoz).intValue();
    String err=p.getProperty("cell.height");
    systemMessage("Argh!");
    float cell_Height = Float.parseFloat(p.getProperty("cell.height"))*mnoz;
    float cell_Width =  Float.parseFloat(p.getProperty("cell.width"))*mnoz;


    private void systemMessage(String message){
       System.out.println(message);
    }

}
Comments