Results 1 to 4 of 4
Thread: printArray error
- 06-01-2010, 05:57 PM #1
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
printArray error
Below is my code and I expect the result is
array1 is <2, 3, 5, 7, 11, 13, 17, 19>
array1 is <0, 3, 2, 7, 4, 13, 6, 19>
but the result shows like:
array1 is: <>
array1 is <>
Please advise. Thanks
Java Code:public static void main(String[] args) { // TODO code application logic here int[] array1 = {2, 3, 5, 7, 11, 13, 17, 19}; int[] array2; System.out.print("array1 is: "); printArray(array1); System.out.println(); array2 = array1; array2[0] = 2; array2[1] = 3; array2[2] = 2; array2[3] = 7; array2[4] = 4; array2[5] = 13; array2[6] = 6; array2[7] = 19; System.out.print("array1 is "); printArray(array1); System.out.println(); } public static void printArray(int[] array) { System.out.print('<'); for (int i = 0; i < array.length; i++) { // print an element //Do the coding here // print a comma delimiter if not the last element //Do the coding here } System.out.print('>'); } }
- 06-01-2010, 06:22 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
I don't know where that zero comes from in your second print; you are hiding something from us; in your actual code you showed us you're not printing anything except for those angular brackets.
kind regards,
Jos
- 06-01-2010, 06:26 PM #3
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
No, I don't hide anything. I don't know why it doesn't show up like my expectation and thus it's great that you can fix and adjust my program to produce as my expected result, so I can learn from it. Thanks
- 06-05-2010, 05:48 PM #4
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
I just solved my error. Below is the complete code:
Java Code:public static void main(String[] args) { // TODO code application logic here int[] array1 = {2, 3, 5, 7, 11, 13, 17, 19}; int[] array2; System.out.print("array1 is: "); printArray(array1); System.out.println(); array2 = array1; array2[0] = 2; array2[1] = 3; array2[2] = 2; array2[3] = 7; array2[4] = 4; array2[5] = 13; array2[6] = 6; array2[7] = 19; System.out.print("array1 is "); printArray(array1); System.out.println(); } public static void printArray(int[] array) { System.out.print('<'); for (int i = 0; i < array.length; i++) { System.out.print(array[i]); if(i <(array.length -1)) { System.out.print(", "); } } System.out.print('>'); } }
Similar Threads
-
> Operator cannot be applied error and return incompatible types error
By corney_16 in forum New To JavaReplies: 1Last Post: 03-10-2010, 01:53 PM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
java.lang.Error: Error opening DSound for capture
By NARs in forum NetworkingReplies: 1Last Post: 10-26-2009, 04:38 PM -
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01-23-2009, 08:53 AM -
error 530 error authentication required
By rgale in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 05-12-2008, 04:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks