Ok, I'm taking a intro to programming with java class, and for the most part I've been able to keep up with the assignments. I'm in the process of writing a statement that will output the cube root of a number, I've come up with the below as what I think is what it will take to do this, I get no errors when I compile it but when I run it, it goes straight to the press any key and exits. Any input will be appreciated, I don't want anyone to solve this for me, just point to a line that is in error and a possible link as to where I can go to learn to fix it. Thanks in advance. K

/***********************************************
* cubeRoot.java
* Tom Smith
*
* This program will output the cube root of a number.
***********************************************/

import java.util.*;
import java.util.Scanner;
import java.io.*;
import static java.lang.Math.*;

   public class cubeRoot {

   public static native double cbrt(double x);

   public static void main(String[] args) {

   if (args.length != 1) System.exit(0);

   double x = Double.parseDouble(args[0]);

	Scanner keyboard = new Scanner(System.in);

	System.out.print("Enter a number: ");

		    x = keyboard.nextInt();
       System.out.printf("The cube root of %f is %f.\n",x, cbrt(x));
     }
   }

Ok, I'm taking a intro to programming with java class, and for the most part I've been able to keep up with the assignments. I'm in the process of writing a statement that will output the cube root of a number, I've come up with the below as what I think is what it will take to do this, I get no errors when I compile it but when I run it, it goes straight to the press any key and exits. Any input will be appreciated, I don't want anyone to solve this for me, just point to a line that is in error and a possible link as to where I can go to learn to fix it. Thanks in advance. K

/***********************************************
* cubeRoot.java
* Tom Smith
*
* This program will output the cube root of a number.
***********************************************/

import java.util.*;
import java.util.Scanner;
import java.io.*;
import static java.lang.Math.*;

public class cubeRoot {

public static native double cbrt(double x);

public static void main(String[] args) {

if (args.length != 1) System.exit(0);

double x = Double.parseDouble(args[0]);

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter a number: ");

x = keyboard.nextInt();
System.out.printf("The cube root of %f is %f.\n",x, cbrt(x));
}
}

I'm not too familiar with printf for Java, so can't help you there, but add this line:

System.out.println (args.length);

at the top of your main function. If it doesn't display 1, then you aren't passing the program the correct parameters and the program is exiting. Frankly I don't see the need to ask for any parameters, because you are immediately overwriting x before you ever do anything with it with:

x = keyboard.nextInt();

Also notice that you have defined x as a double and you are grabbing and integer from Scanner.

If you are not including arguments on the command line when you execute the program, then this line will exit the program immediately

if (args.length != 1) System.exit(0);

Edit: Posted the same time as VernonDozier. He mentions the same.

Ok, I see where I made that mistake, and have made some changes, now I'm getting an exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at cubeRoot.main.

/***********************************************
* cubeRoot.java
* Tom Smith
*
* This program will output the cube root of a number.
***********************************************/

import java.util.*;
import java.util.Scanner;
import java.io.*;
import static java.lang.Math.*;

   public class cubeRoot {

   public static native double cbrt(double number);

   public static void main(String[] args) {

   //if (args.length != 1) System.exit(0);

   double number = Double.parseDouble(args[0]);

   System.out.println (args.length);

	//number = Math.cbrt(number);

	Scanner keyboard = new Scanner(System.in);

	System.out.print("Enter a number: ");

		    number = keyboard.nextDouble();
       System.out.printf("The cube root of %f is %f.\n",number, cbrt(number));

     }
   }

You have mixed two different ways of giving input to the program together but you haven't separated them to be independent of one another.

Everything that deals with the args[] is working with parameters that were passed on the command line when you execute the program.

The Scanner code is used to collect input from the user while the program is running.

The code that is expecting args[] will fail if there are none, which is why you are getting that index error. You can't access the first element (0) of an array that is empty.

You need to either separate the two input processing sections, perhaps with an if statement

if (args.length > 0){
   // use the args
} else {
  // use the Scanner
}

Or delete the code that is relying on args.

This article has been dead for over six months. Start a new discussion instead.