| | |
Noob and unsure
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 2
Reputation:
Solved Threads: 0
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
java Syntax (Toggle Plain Text)
/*********************************************** * 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)); } }
Last edited by cscgal; Sep 16th, 2008 at 9:47 pm. Reason: Added code tags
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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:
Java Syntax (Toggle Plain Text)
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 Edit: Posted the same time as VernonDozier. He mentions the same.
Java Syntax (Toggle Plain Text)
if (args.length != 1) System.exit(0);
Last edited by Ezzaral; Sep 16th, 2008 at 3:38 pm.
•
•
Join Date: Sep 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
java Syntax (Toggle Plain Text)
/*********************************************** * 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)); } }
Last edited by cscgal; Sep 16th, 2008 at 9:47 pm. Reason: Added code tags
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 Or delete the code that is relying on args.
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
Java Syntax (Toggle Plain Text)
if (args.length > 0){ // use the args } else { // use the Scanner }
![]() |
Similar Threads
- Noob needs help i/o file data (Python)
- Please Help Virus Problem (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: Bubble sorting
- Next Thread: Time of Day Script
| Thread Tools | Search this Thread |
addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class classes client code collision component crashcourse css csv database eclipse ee error exception fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan loop machine map method methods migrate mobile netbeans newbie output phone physics print problem program programming project radio recursion reporting robot scanner screen se server service set size sms socket software sort sql string swing textfield threads transfer tree trolltech ubuntu utility windows






