Join 149,519 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,378 people online right now. Registration is fast and FREE... Join Now!
public class LoopDriver { public static void main ( String[] args ) { int [] valA = {1,2,3,4,5,6,7,8,9,10}; int [] valB = {0,2,4,5,8,10,12,14,16,18,20}; int [] valC = {1,4,9,16,25,36,49,64,81,100};
for (int i = 0; i < 10; i++ ) valA[i] = i + 1; System.out.println(valA);
for (int i = 0; i < 10; i++ ) valB[i] = 2 * i; System.out.println(valB);
for ( int i = 0; i < 10; i++) valC[i] = (i + 1) * (i + 1); System.out.println(valC);
int[] valD = new int[10]; System.out.println(valD);
public class LoopDriver { public static void main ( String[] args ) { int [] valA = {1,2,3,4,5,6,7,8,9,10}; int [] valB = {0,2,4,5,8,10,12,14,16,18,20}; int [] valC = {1,4,9,16,25,36,49,64,81,100}; int [] valD = {0,0,0,0,0,0,0,0,0,0}; int [] valE = { 1, 4, 9, 16, 9, 7, 4, 9, 11 }; { for (int i = 0; i < 10; i++ ) { valA[i] = i + 1; System.out.print(valA[i]); } } { for (int i = 0; i <= 10; i++ ) { valB[i] = 2 * i; System.out.print(valB[i]); } } for ( int i = 0; i < 10; i++) { valC[i] = (i + 1) * (i + 1); System.out.print(valC[i]); }
{ for (int i = 0; i<10; i++) System.out.print(valD[i]); }
{ for (int i = 0; i<9; i++) System.out.print(valE[i]); }
} }
returns: 1234567891002468101214161820149162536496481100000000000014916974911 should return: 12345678910 02468101214161820 149162536496481100 0000000000 14916974911
Thanks, I made corrections but the output is not behaving the way I want it to. "bad output" Can anyone tell me how to get it to look like the way I think it should return? I have tried different combinations of print vs. println and nothing seems to work for me. I have also tried to put all print statements at the end it did not work either. When I tried to lump the print statements together, I received a error " illegal start of expression"
public class LoopDriver { public static void main ( String[] args ) { int [] valA = {1,2,3,4,5,6,7,8,9,10}; int [] valB = {0,2,4,5,8,10,12,14,16,18,20}; int [] valC = {1,4,9,16,25,36,49,64,81,100}; int [] valD = {0,0,0,0,0,0,0,0,0,0}; int [] valE = { 1, 4, 9, 16, 9, 7, 4, 9, 11 }; { for (int i = 0; i < 10; i++ ) { valA[i] = i + 1; } System.out.print(valA[i]);// here is where I get the error and will not compile } { for (int i = 0; i <= 10; i++ ) { valB[i] = 2 * i; } System.out.print(valB[i]); } { for ( int i = 0; i < 10; i++) { valC[i] = (i + 1) * (i + 1); } System.out.print(valC[i]); } { for (int i = 0; i<10; i++) { System.out.print(valD[i]); } } { for (int i = 0; i<9; i++) { System.out.print(valE[i]); } } } }
This post has been edited by snake: 29 Jun, 2007 - 09:48 AM
what compiler are you using as this error should have been basic to solve... You define i in the scope of the for loop, then you try and use it outside.... think about the problem with that....
Also pennyBoki did not say to move your print statements, he said to "add" a System.out.println(); statement between your loops, not to put a System.out.println(valA[i]); statement outside... as this breaks the rule in my first point.
Also your tabbing style is horrible at best... again which compiler are you using?
There are many, many, many extra sets of {} why are you using these? They have no meaning how you have them setup.
I'm guessing you do not know the difference between print and println, the first prints on 1 line, continuously while the second will start a new line after it prints. The same as using a "\n" command to create a newline.
Here is your code corrected, any other formatting of your output is up to you, you should be able to fix it from here....
CODE
public class LoopDriver { public static void main ( String[] args ) { int [] valA = {1,2,3,4,5,6,7,8,9,10}; int [] valB = {0,2,4,5,8,10,12,14,16,18,20}; int [] valC = {1,4,9,16,25,36,49,64,81,100}; int [] valD = {0,0,0,0,0,0,0,0,0,0}; int [] valE = { 1, 4, 9, 16, 9, 7, 4, 9, 11 };
for (int i = 0; i < 10; i++ ) { valA[i] = i + 1; System.out.print(valA[i]); } System.out.println(); for (int i = 0; i <= 10; i++ ) { valB[i] = 2 * i; System.out.print(valB[i]); } System.out.println(); for ( int i = 0; i < 10; i++) { valC[i] = (i + 1) * (i + 1); System.out.print(valC[i]); } System.out.println(); for (int i = 0; i<10; i++) { System.out.print(valD[i]); } System.out.println(); for (int i = 0; i<9; i++) { System.out.print(valE[i]); } } }
Thank you but why and what does the System.out.println(); do ? And yes now I see what you meant about just adding the line. Once I applied what you said it all worked fine. Thanks again.
System.out.println(); means printing an empty line.
e.g. I want the following output:
Dream
In
Code
I would use this code:
CODE
System.out.println("Dream");//here only Dream will be printed no matter what System.out.println();//here an empty line is printed System.out.println("In");//here only In //is printed and the rest of the line is empty
System.out.println();//empty line System.out.println("Code");//only Code is printed
NOTE: println() is not same as print() For further reading visit THIS link.
This post has been edited by PennyBoki: 29 Jun, 2007 - 02:27 PM