0

So, code below, that takes serial numbers as arguments from txt file, works correctly on my computer. Every number is written on one line. So, here is the code:

import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;

public class Main {
    public static int fiboComputingAct(int serial){
        if (serial == 1 || serial == 2) {
            return 1;
        }else{
            int nMinus2 = 1;
            int nMinus1 = 1;
            int result = 0;
            for (int i = 3; i <= serial; i++){
                result = nMinus1 + nMinus2;
                nMinus2 = nMinus1;
                nMinus1 = result;   
            }
            return result;
        }   
    }

    public static void main (String [] args){
        try {
            File textFile = new File("texts/1.txt"); //"texts/1.txt" will be replaced on args[0] in codeeval
            Scanner scan = new Scanner(textFile);
            int fiboSerialNumber;
            while (scan.hasNextLine()) {
                fiboSerialNumber = scan.nextInt();
              System.out.println(fiboComputingAct(fiboSerialNumber));
                }
            } catch (Exception e) {JOptionPane.showMessageDialog(null, "File is not found");}   
        }   
    }

But it doesn't work in CodeEval. That's what the site compiler say:

Fontconfig error: Cannot load default config file Exception in thread "main" java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires it. at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:207) at java.awt.Window.(Window.java:535) at java.awt.Frame.(Frame.java:420) at java.awt.Frame.(Frame.java:385) at javax.swing.SwingUtilities$SharedOwnerFrame.(SwingUtilities.java:1759) at javax.swing.SwingUtilities.getSharedOwnerFrame(SwingUtilities.java:1834) at javax.swing.JOptionPane.getRootFrame(JOptionPane.java:1697) at javax.swing.JOptionPane.showOptionDialog(JOptionPane.java:863) at javax.swing.JOptionPane.showMessageDialog(JOptionPane.java:667) at javax.swing.JOptionPane.showMessageDialog(JOptionPane.java:638) at javax.swing.JOptionPane.showMessageDialog(JOptionPane.java:609) at Main.main(Main.java:45)

SanchelliosProg
  • 2,091
  • 3
  • 20
  • 32

1 Answers1

0

According to the stack trace, the error is occurring here:

JOptionPane.showMessageDialog(null, "File is not found");

Taking a look at the documentation for the HeadlessException, we see that it is:

Thrown when code that is dependent on a keyboard, display, or mouse is called in an environment that does not support a keyboard, display, or mouse.

A bit more research makes me believe that CodeEval is probably trying to execute your code in a headless linux environment.

To avoid this error, just replace the graphical error reporting with a simple e.printStackTrace() in your catch block.

Community
  • 1
  • 1
azurefrog
  • 10,785
  • 7
  • 42
  • 56