Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Strings and command line arguments  RSS feed

 
Kezia Matthews
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How do you find the length of the whole string passed in the command line, if more than one argument is passed?
Thanks,
Kezia.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kezia Matthews:
Hi,
How do you find the length of the whole string passed in the command line, if more than one argument is passed?
Thanks,
Kezia.

hi,
You mean how many strings in command line? If so,
args.length will give you that value (String[] args).
victor
 
Kezia Matthews
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, not the number of arguments.
If I pass the string "This is Java", I want to know, what will give me the total length of the above string i.e, 12
Kezia.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kezia Matthews:
No, not the number of arguments.
If I pass the string "This is Java", I want to know, what will give me the total length of the above string i.e, 12
Kezia.

Kezia:
You jave no way to determine the len of the whole
command line, cuz # of spaces between argments are
not important. Trty this when you run your prog:
this is java
That gives you same results while the length of
the whole command line changes. You can only know
the length of every string argument.
Hope this helps.
victor
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every space given on the command line will separate the arguments, thus This is Java will be seen as three arguments. If you want to know how much characters are in the String just replace the space by another character (an underscore _ for instance).
This_is_Java will be taken as one argument, and then you can query its length with args[0].length(). Remember that args[0] is a String object
HIH
 
Rick Salsa
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the length() method, it returns an int:

That should do the trick or args[0].length() will work for reading off of the command line.
/rick
[ January 15, 2002: Message edited by: rick salsa ]
 
Simon Whitehouse
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there
Is this the sort of thing that you meant :-

It's not pretty but it works for me.
<Edited as I realised you wanted to include the spaces in the command line as well>
hth
Simon
[ January 15, 2002: Message edited by: Simon Whitehouse ]
 
Kezia Matthews
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Simon. This is what I want.
But, I did not want to use a loop. I guess that it is not possible without a loop.
Thanks anyway.
Kezia.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You won't go very far in life without using loops
Or the verb "to be" for that matter
Rob
 
Kezia Matthews
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wondered if Java provided an easier way out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!