Welcome to Dream.In.Code
Become a Java Expert!

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!




confusing output from arrays

 
Reply to this topicStart new topic

confusing output from arrays, Can anyone tell me why the outcome is the way it is intead of the numb

snake
28 Jun, 2007 - 07:03 AM
Post #1

New D.I.C Head
*

Joined: 14 Jun, 2007
Posts: 31


My Contributions
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};
    
     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);
    
     int [] valE = { 1, 4, 9, 16, 9, 7, 4, 9, 11 };
     System.out.println(valE);
            
  }
}

returns:
[I@20fa83
[I@11eb199
[I@1de498
[I@8ae45a
[I@95da38




Can anyone help with why the display returns what it does instead of what is in each array?
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Confusing Output From Arrays
28 Jun, 2007 - 07:31 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
You can't print arrays like that. Inside the loop do the printing like:
CODE
for (int i = 0; i < 10; i++ )
{
      valA[i] = i + 1;
     System.out.println(valA[i]);
}


This post has been edited by PennyBoki: 28 Jun, 2007 - 07:32 AM
User is offlineProfile CardPM
+Quote Post

snake
RE: Confusing Output From Arrays
28 Jun, 2007 - 10:01 AM
Post #3

New D.I.C Head
*

Joined: 14 Jun, 2007
Posts: 31


My Contributions
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]);
     }  
    }
    {
     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"
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Confusing Output From Arrays
28 Jun, 2007 - 04:51 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Just outside every for loop make a
System.out.println();
User is offlineProfile CardPM
+Quote Post

snake
RE: Confusing Output From Arrays
29 Jun, 2007 - 03:51 AM
Post #5

New D.I.C Head
*

Joined: 14 Jun, 2007
Posts: 31


My Contributions
When I do that I get an error saying that variable i is not declared.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Confusing Output From Arrays
29 Jun, 2007 - 05:37 AM
Post #6

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
post the new code please
User is offlineProfile CardPM
+Quote Post

snake
RE: Confusing Output From Arrays
29 Jun, 2007 - 09:38 AM
Post #7

New D.I.C Head
*

Joined: 14 Jun, 2007
Posts: 31


My Contributions
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]);// 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
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Confusing Output From Arrays
29 Jun, 2007 - 10:05 AM
Post #8

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
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]);
          }
      }
}

User is offlineProfile CardPM
+Quote Post

snake
RE: Confusing Output From Arrays
29 Jun, 2007 - 12:32 PM
Post #9

New D.I.C Head
*

Joined: 14 Jun, 2007
Posts: 31


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Confusing Output From Arrays
29 Jun, 2007 - 02:25 PM
Post #10

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:16PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month