• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

operators

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class c {
public static void main(String[] args) {
int i = 0;
while (++i < args.length) {
System.out.print(args[i]);
}}}

when i run this program like java c abcdef,it doesn't show any output.why is it like that?
 
lavanya sankuappan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am sorry about that,i didn't leave space between arguments
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you using while (++i < args.length) first value of i in boolean expression after ++ is 1. When you start program with one argument (like java c arg), program no output. When you start program with two arguments (like java c arg1 arg2), program output without last argument.

Correct code:
class c {
public static void main(String[] args) {
int i = 0;
while (i++ < args.length) {
System.out.print(args[i]);
}}}
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello lavanya
ur program will give an output just try and hey vladimir program must give an exception ArrayIndexoutOfBound
ur program is all right it shows output but leave first element
output:bcdef

krish bajaj
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai lavanya,
you dont get an output because the While test is false so it never goes to the print statement.
While(++i<args.length) ie. 1<1 is false.
Hope this helps
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two Topic here coverd
1) Run time Argument will start with 0
2) Pre Increment Operator ++

++i Will increate the i before it will compare.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The prefix increment operator increments the value of the variable first and then uses the variable in the operation[or expression]. Now consider your case when u say ++i<args.length and the command line exp is java c abcd. here args.length will return 1. value of i before the if() loop is zero. when it encounters the line ++i < args.length, it first increments the value of i to 1 and then i takes part in the expression. Now 1 cannot be less than 1 and hence no output is shown.
 
reply
    Bookmark Topic Watch Topic
  • New Topic