Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Netbeans and Arguments (n00bish question)
#1
Ok so this is a really n00bish question but i cant seem to find an answer for this.

Here is my source code:
Code:
class NewRoot {
    public static void main(String[] args){
        int number = 100;
        if (args.length > 0) {
            number = Integer.parseInt(args[0]);
        }
        System.out.println("The square root of "
                + number
                + " is "
                + Math.sqrt(number));
    }
}
I went to Run>Set Project Configuration>Customize and changed the argument to 169. I ran the file and it gives me the output of
Code:
The square root of 100 is 10.0

My question is why is it not taking the argument of 169 instead of evaluating 100? I checked the length of the argument and its 3. Any help would be appreaciated. Dont be to harsh im still begining Java.
Reply
#2
Well I'm no Java expert but the only function argument you have is:
Code:
(String[] args)
If you want it to evaluate a different number you can just manually change
Code:
int number = 100;
Or you can make it ask for user input.
For example, running this code:
Code:
class NewRoot {
    public static void main(String[] args){
        int number = 25;
        if (args.length > 0) {
            number = Integer.parseInt(args[0]);
        }
        System.out.println("The square root of "
                + number
                + " is "
                + Math.sqrt(number));
    }
}

Makes it output the square root of 25.
Similarly, this script asks for a number from the user using a scanner object:
Code:
import java.util.*;

class NewRoot {
    public static void main(String[] args){
        Scanner numscan = new Scanner(System.in);
        System.out.print("What number do you want the square root of?:");
        int number = numscan.nextInt();
        if (args.length > 0) {
            number = Integer.parseInt(args[0]);
        }
        System.out.println("The square root of "
                + number
                + " is "
                + Math.sqrt(number));
    }
}
The output is:
Code:
What number do you want the square root of?:25
The square root of 25 is 5.0
Hope this helped!
More on scanner objects here
[Image: izsyo6.jpg]


Reply
#3
Ok that helps a little bit but maybe i should have worded it my question a little better. Before I ran it i changed the arguments in NetBeans to "169". So when the file tests
Code:
if(args.length > 0){
which should test as true. Afterwards it should run
Code:
number = parse.Interger(args[0]);
. This changes or casts the value of "number" to the arguments given to the program, because the length of 169 is = 3. So then after this it should run the last line which should display the output of
Code:
The square root of 169 is 13
. Why is it not changing the value of "number" to the argument of 169. Maybe I'm missing something? Is there anything that would make my theory illogical? I like your suggestions though, much appreaciated.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  netbeans error ?? prince76 1 1,081 03-22-2012, 08:56 AM
Last Post: redpois0n
  How do I turn off auto-format and auto-resize in Netbeans? nevets04 0 2,062 12-01-2009, 07:20 PM
Last Post: nevets04

Forum Jump:


Users browsing this thread: 1 Guest(s)