0

I want to write a simple program (using the type double) that checks if the number is negative or not.

I get the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at NegativeZahl.main(NegativeZahl.java:7)

    import javax.swing.JOptionPane;

    public class NegativeZahl {

     public static void main(String[] args) {
     String eingabe;
     double zahl = Double.parseDouble(args[0]);
     eingabe = JOptionPane.showInputDialog("Geben Sie eine Zahl ein!");
     if ( zahl >= 1) {
         JOptionPane.showMessageDialog(null,zahl + "Die Zahl ist    positiv" );
     } else {
         JOptionPane.showMessageDialog(null, zahl + "Die Zahl ist negativ");
     }

    }

   }
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Larissa
  • 1
  • 1

2 Answers2

1

Your program mixes up two ways to get the number:

You fill the value zahl from the command line args[0], meaning you have to run your program with something like java NegativeZahl 123.4 with 123.4 being the number you want to test.

But you also open a dialog to ask the user for entering a number, but then do nothing with his input eingabe.

The error looks like you didn't provide the number on the command line.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7
-2

I believe that you haven't run this program with argument like java program_name