-2

I don't know what ist wrong, it can read it, but then it tells me Index 1 out of bounds for length 0. What does it mean?

public class Kreisberechnung3 {
  public static void main(String[] args) {
    String einheit = args[1];
    double radius = Double.parseDouble(args[0]);
    double umfang = 2.0 * 3.1415926 * radius;
    double flaeche = 3.1415926 * radius * radius;
    System.out.print("Umfang: ");
    System.out.print(umfang);
    System.out.println(" " + einheit);
    System.out.print("Fläche: ");
    System.out.print(flaeche);
    System.out.println(" " + einheit + '\u00b2');
  }
}
Leonidas
  • 9
  • 2
  • 1
    it means you are trying to get the second element of an empty array. you'll need to provide (enough) command line parameters for this to work – Stultuske Dec 04 '18 at 08:05
  • 2
    How do you launch your application ? – Arnaud Dec 04 '18 at 08:05
  • Try to use `args[0]` instead of `args[1]` – flyingfox Dec 04 '18 at 08:05
  • @lucumt then he would still be trying to get the first element of an empty array. same problem – Stultuske Dec 04 '18 at 08:06
  • @Stultuske The OP need to do some check before access the element – flyingfox Dec 04 '18 at 08:07
  • which changes nothing about your remark wouldn't solve the issue – Stultuske Dec 04 '18 at 08:07
  • It is because you are not passing an argument to the command line. You are looking for 2 command line arguments `String einheit = args[1];` and `double radius = Double.parseDouble(args[0]);` but when running it you are probably not passing anything. – jbx Dec 04 '18 at 08:23

1 Answers1

1
public static void main(String[] args) {
    String einheit = args[1];

This second line tries to extract a String from the String array args. This array contains the command line parameters.

When running the code through a command prompt, you would run something like:

java Kreisberechnung3

Now let's say you would want two elements in the array args, "first" and "second" you would need to update that to:

java Kreisberechnung3 first second

String s = args[1];

keeping in mind that in Java, an array is zero based, this would get the second element, making the value s refers to: "second".

If you run your code through an IDE, you'll need to check your IDE on how to pass these parameters.

Stultuske
  • 9,296
  • 1
  • 25
  • 37