-2

I'm reading Sams Teach Yourself Java in 24 Hours and came across this 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));
   }
}

But in order for the code to be compiled, the writer enters 169 in the Arguments field in the

Run>Set Project Configuration>Customize

menu (in NetBeans IDE).

So my question is: what's the purpose of the specific field? Does 169 means something or is it just a random number? It's a pity the writer doesn't say anything about it!

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
HappyCane
  • 363
  • 1
  • 2
  • 10
  • Really I doubt anyone would able to give good help here because, we don't know what book you're reading and what is the context of that code. Maybe re-read the chapter again to know what author is conveying and what does that program do. – Pradeep Simha Nov 16 '14 at 16:56
  • 1
    +1 for improving your question. I took the liberty of removing the line numbers and formatting your code for easier reading. And welcome to StackOverflow! – Hovercraft Full Of Eels Nov 16 '14 at 18:35

2 Answers2

1

The author is giving you an example of running a program with arguments given via the terminal. This is usually done in your terminal or command prompt by running the code as such

javac ProgramName.java
java ProgramName <arguments>

Since you are writing and running your program in Netbeans, and wont be using the terminal, you can configure to run the project with a given command line argument. This is what you are doing in the netbeans menus.

The String "169" only has meaning for the given program. The author is trying to demonstrate how the program will run given a command line argument, in this case he sets it to an arbitrary value "169." In your code you are taking this String and turning it into an int.

bmcentee148
  • 594
  • 1
  • 7
  • 18
  • So unless mentioned otherwise in the program, any input from the console is registered in the arguments array? – HappyCane Nov 16 '14 at 17:39
  • Each argument AFTER the ProgramName will go into the String[] args and can be called upon in the program using the form args[i] where i is the index of the argument you would like to refer to. But remember they are all stored in memory as String type. – bmcentee148 Nov 16 '14 at 18:04
  • Also note that args is just the conventional and preferred name for your String[] of command ling arguments. TECHNICALLY, you could name it whatever you want. I do NOT recommend doing so but I feel like knowing this gives you a better idea of the main method generally is. – bmcentee148 Nov 16 '14 at 18:07
1

The number 169 is almost certainly meaningless and arbitrary; it is used by the author merely as an example. Now let's break the code down line by line to address your concerns.

args contains any command line arguments as an array of strings:

public static void main(String[] args) {

The author declares a variable of type int and calls it number. He assigns an initial value of 100. This would appear to be a randomly selected number to demonstrate the concept - a common approach in programming books.

int number = 100;

He then checks if there were any command line arguments supplied; if there were, args.length will be greater than zero:

if (args.length > 0)

If there is a command line argument he parses the first argument into the number variable (this operation could fail by the way if you supply a non-numeric first argument):

{
   number = Integer.parseInt(args[0]);
}

Note that if there is no command line argument, number is not overwritten. So, call the program without a command line argument and it will display the square root of 100. Call it with an argument of 169 (surely another number picked out of the air) and it will show you the square root of 169.

Command line arguments will be packed into args; input from keypresses etc after the program has started will not.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108