Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Printing problem

  Asked By: Brayden    Date: Jul 05    Category: Java    Views: 7005
  

code:
-----
for(int i=0; i<=9; i++)
System.out.println("x[i] = "+x[i]);

you know i was expecting x[0]= value
x[1]= value

-----
x[9]= value

its just formatted output....

i know i did a mistake...it should be

System.out.println("x["+i+"] = "+x[i]);

i dont know why "+i+" is needed?

this could have been x[+i] or x[i].... but why x["+i+"] ??????

so actually i want to know what is the rule for formatting here.
if it had been C language...i could have written...
...printf("x[%d]",i,x[i]);

what is the rule for formatted output in java???

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Adaulfo Fischer     Answered On: Jul 05

println takes only one variable. In this case u need to convert the
result into a String.
first string is "x["
Second is value of i (if use + with any string it automatically
converts second parameter into string)
third string is "] = "
last value of x[i]

it is not "+i+" it is "x[" + i +"] = " + x[i];
----------------------- string+ var +string+var -> string

 
Didn't find what you were looking for? Find more on Printing problem Or get search suggestion and latest updates.




Tagged: