1

I am reading through the java tutorials, and I don't understand when it says

"The following is the output from the program when you use 4.5 and 87.2 for the command-line arguments:"

What I mean is how do I pass the values to the program. A piece of the code is this.

float a = (Float.valueOf(args[0])).floatValue();
float b = (Float.valueOf(args[1])).floatValue();

I have tried changing "args[0]" to "4.5" and "args[1]" to "87.2" which are the given values from this page.

https://docs.oracle.com/javase/tutorial/java/data/converting.html

Upon doing so I receive "requires two command-line arguments." which is the else part of the code. I'm pretty sure I am being oblivious to this. I have tried looking for anything regarding passing arguments but i can't find exactly what to do.

I have also tried creating two "string" values named one and two with the same values as above and inputting the string name into the args positions but still received the same outcome.

Is it something simple such as requesting an input from the user or should I manually put the values in there and if I need to add the values into the argument then how would I go about doing so.

Morgan
  • 87
  • 10
  • possible duplicate of [What is "String args\[\]"? parameter in main method Java](http://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java) – Radiodef Apr 08 '15 at 03:51

3 Answers3

2

The arguments passed to the main methods are the one typed when starting your java application from command line. An revelant example for your case would be :

java YourProgram 4.5 87.2

Then you will be able to access them from args[0] and args[1] as explained in the tutorial.

For more examples read the Command-Line arguments part of the java tutorial.

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
  • 1
    I see now, so its under command prompt when you actually execute the program. Kind of like adding parameters for execution. I have been using Netbeans so I was struggling to figure it out but now that I see that it makes sense. Thank you. – Morgan Apr 08 '15 at 03:56
0

If you're running the program from a command line (typing something like java ValueOfDemo into a terminal), you would type java ValueOfDemo 4.5 87.2 to pass 4.5 and 87.2 as the first and second arguments, respectively. If you're running the program using an IDE such as Eclipse or NetBeans, search for that program's documentation on how to pass command line arguments to the program.

In general, command line arguments are arguments that are passed to the program you're running when the program is started. You can also ask the user for input while your program is running, but you would explicitly write code to do so and accept the value.

See this page for more information: https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html

Cass
  • 870
  • 8
  • 21
-1

Open cmd(window key + r) and compile by command: javac yourClass.java and then execute by command: java yourClass 4.5 87.2 you will see result

angel
  • 41
  • 1
  • 6