This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

basics...index into array

 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Got stumped early on working through a java book (B.Eckel). Can anyone explain what it means to index into the command line array of String? This is not homework, I'm working on my own.
Thanks.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can pass parameters into a java app from the command line. They are read into the array when you start your main "public static void main (String args[])". Spaces in the command line are the delimiters. You can reference these parameters by referencing args[0] or args[1] or args[2]...for however many parameters you've passed in.
Norm
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Pauline McNamara:
Hello,
Got stumped early on working through a java book (B.Eckel). Can anyone explain what it means to index into the command line array of String? This is not homework, I'm working on my own.
Thanks.



Hi Pauline,
What he means is, to use an index (variable to go through each
element of the array). so if a is an array of 10 elements., we can denote an element of the array by a[i], with i, the index, going from 1 to 10. so, since args is also an array, args[0] would be the first element of the array...args is a special array in that it holds whatever we type at the
command line, when we run the program...and holds it as a string. hence the declaration String args[] in main(). so if u were to say java somefile hi there, then args[0] would be "hi", and args[1] would be "there".
the thing to remember is that even if u enter a number, instead of hi or there would be treated as a string.
so to print out those 2 words, all we need to say is println
args[0] and println args[1].
hope this made sense.
good luck.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!