Noob and unsure

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 2
Reputation: kogues is an unknown quantity at this point 
Solved Threads: 0
kogues kogues is offline Offline
Newbie Poster

Noob and unsure

 
0
  #1
Sep 16th, 2008
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
  1. /***********************************************
  2. * cubeRoot.java
  3. * Tom Smith
  4. *
  5. * This program will output the cube root of a number.
  6. ***********************************************/
  7.  
  8. import java.util.*;
  9. import java.util.Scanner;
  10. import java.io.*;
  11. import static java.lang.Math.*;
  12.  
  13. public class cubeRoot {
  14.  
  15. public static native double cbrt(double x);
  16.  
  17. public static void main(String[] args) {
  18.  
  19. if (args.length != 1) System.exit(0);
  20.  
  21. double x = Double.parseDouble(args[0]);
  22.  
  23. Scanner keyboard = new Scanner(System.in);
  24.  
  25. System.out.print("Enter a number: ");
  26.  
  27. x = keyboard.nextInt();
  28. System.out.printf("The cube root of %f is %f.\n",x, cbrt(x));
  29. }
  30. }
Last edited by cscgal; Sep 16th, 2008 at 9:47 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Noob and unsure

 
0
  #2
Sep 16th, 2008
Originally Posted by kogues View Post
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:
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,478
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: Noob and unsure

 
0
  #3
Sep 16th, 2008
If you are not including arguments on the command line when you execute the program, then this line will exit the program immediately
  1. if (args.length != 1) System.exit(0);
Edit: Posted the same time as VernonDozier. He mentions the same.
Last edited by Ezzaral; Sep 16th, 2008 at 3:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 2
Reputation: kogues is an unknown quantity at this point 
Solved Threads: 0
kogues kogues is offline Offline
Newbie Poster

Re: Noob and unsure

 
0
  #4
Sep 16th, 2008
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.
  1. /***********************************************
  2. * cubeRoot.java
  3. * Tom Smith
  4. *
  5. * This program will output the cube root of a number.
  6. ***********************************************/
  7.  
  8. import java.util.*;
  9. import java.util.Scanner;
  10. import java.io.*;
  11. import static java.lang.Math.*;
  12.  
  13. public class cubeRoot {
  14.  
  15. public static native double cbrt(double number);
  16.  
  17. public static void main(String[] args) {
  18.  
  19. //if (args.length != 1) System.exit(0);
  20.  
  21. double number = Double.parseDouble(args[0]);
  22.  
  23. System.out.println (args.length);
  24.  
  25. //number = Math.cbrt(number);
  26.  
  27. Scanner keyboard = new Scanner(System.in);
  28.  
  29. System.out.print("Enter a number: ");
  30.  
  31. number = keyboard.nextDouble();
  32. System.out.printf("The cube root of %f is %f.\n",number, cbrt(number));
  33.  
  34. }
  35. }
Last edited by cscgal; Sep 16th, 2008 at 9:47 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,478
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: Noob and unsure

 
0
  #5
Sep 16th, 2008
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
  1. if (args.length > 0){
  2. // use the args
  3. } else {
  4. // use the Scanner
  5. }
Or delete the code that is relying on args.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC