about printf please help having errors
Code java:
System.out.print("\nList 2 : ");
for (i = 0 ; i<list.length ; i++){
list2 [i] =(int)(99*Math.random()+1);
System.out.printf("\n%2d",list2[i] + " ");
}
i want to display random nos. with a value of 1-99but everytime a single digit number comes up it ruins the alignment so i figured using printf but still im having errors.. what am i doing wrong? i want to cast each number into 2 places so that alignment will be no problem...
Re: about printf please help having errors
Do you have a compiler? Because there are some errors that should come up.
First off, System.out.printf("\n%2d",list2[i] + " ");. You are sending the printf statement a string, not an int. You are doing this by saying: list2[i] + " ".
Second, why are you incrementing the loop based on the size of list, but using list2 inside your loop instead? That is a dangerous practice because those loops might not have the same length.
Third, if you are wanting to just make it all left-justified,
Code java:
for (int i = 0 ; i<list.length ; i++)
{
list [i] =(int)(99*Math.random()+1);
System.out.printf("\n%2d",list[i]);
}
works.
If you are wanting leading zeros however, this:
Code java:
for (int i = 0 ; i<list.length ; i++)
{
list [i] =(int)(99*Math.random()+1);
System.out.printf("\n%02d",list[i]);
}
works.
Based on your style, I can only assume you know C. If that is true, then all of these issues are Syntax Issues that are the exact same in C and you might want to review some of the basic programming syntax that is used in most languages.
Re: about printf please help having errors
oh thanks its working now...
so all i needed to do is get rid of the +" "; after the array
thank you and yeah thanks again for the advice ill be more careful next time with my practices..
and nope i dont know C actually this is my 1st java course so yeah:D:)>-