Bifalgorithm post · Computer science one · The javamain() Method

How are command line arguments accessed and converted in Java?

How are command line arguments accessed and converted in Java? (Computer science one · The javamain() Method) — "The number of arguments provided can be determ…

Ideas in this flowchart

  • Zugriff auf und Verarbeitung von Befehlszeilenargumenten
  • Die Anzahl der Argumente wird mit der Eigenschaft args.length bestimmt.
  • args.length ist eine Ganzzahl, die die Anzahl der bereitgestellten Argumente angibt.
  • Der Programmname beziehungsweise der Klassenname wird nicht mitgezählt.
  • Ein einzelnes Argument wird über seinen Index im Array abgerufen.
  • Das erste Argument hat den Index 0 und wird mit args[0] abgerufen.
  • Das zweite Argument wird mit args[1] abgerufen.
  • Das letzte Argument wird mit args[args.length-1] abgerufen.
  • Argumente liegen im args-Array ausschließlich als String-Werte vor.
  • Für numerische Eingaben müssen die Argumente in Zahlentypen umgewandelt werden.
  • Dafür werden Parsing-Methoden der Klassen Integer und Double verwendet.
  • Integer.parseInt(args[0]) wandelt das erste Argument in eine Ganzzahl um.
  • Double.parseDouble(args[1]) wandelt das zweite Argument in eine Gleitkommazahl um.
Feed/

How are command line arguments accessed and converted in Java?

No cover

Channel

Computer science one

1 page

"The number of arguments provided can be determined using the length property of the array. Specifically, args.length is an integer indicating how many arguments were provided. This does not include the name of the program (class) itself as that is already be known to the programmer. To access any one argument, it will be necessary to index the array. The index for the first argument is zero, thus the first argument is args[0] , the second is args[1] , etc. The last one would be at args[args.length-1] . If a user is expected to provide numbers as input, they’ll need to be converted as the args array are only String types. To convert the arguments you can use parsing methods provided by the Integer and Double classes. An example:\n\n1 //converts the \"first\" command line argument to an integer\n\n2 int x = Integer.parseInt(args[0]);\n\n3 //converts the \"third\" command line argument to a double:\n\n4 double y = Double.parseDouble(args[1]);"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.