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

Join 150,416 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,023 people online right now. Registration is fast and FREE... Join Now!




Displaying An Array

 
Reply to this topicStart new topic

Displaying An Array

Runesmith
2 Apr, 2008 - 04:45 PM
Post #1

New D.I.C Head
*

Joined: 7 Mar, 2008
Posts: 15

Hello, everyone. I've written up the code for an array that randomly gathers a hundred different integers between 1 and 10, but I can't figure out how to display the array. The textbook I have is pretty bad about describing how to use arrays; I had a difficult enough time writing the bulk of the code. The rest of the code should work perfectly, though. I'd appreciate any and all help. smile.gif

CODE

package array;
/**
*
* @author Runesmith
*/
public class Main {

    
    public static void main(String[] args) {
        int[] count = new int[10];
        int num;
        for(int i = 0; i < 10; i++){
            num = (int)Math.random() * 10;
            switch(num){
                case 0:
                    count[0]++;
                    break;
                case 1:
                    count[1]++;
                    break;
                case 2:
                    count[2]++;
                    break;
                case 3:
                    count[3]++;
                    break;
                case 4:
                    count[4]++;
                    break;
                case 5:
                    count[5]++;
                    break;
                case 6:
                    count[6]++;
                    break;
                case 7:
                    count[7]++;
                    break;
                case 8:
                    count[8]++;
                    break;
                case 9:
                    count[9]++;
                    break;
                case 10:
                    count[10]++;
                    break;
                
              
              
            }
        }
    }
}

User is offlineProfile CardPM
+Quote Post

Locke37
RE: Displaying An Array
2 Apr, 2008 - 05:06 PM
Post #2

Contributor of the Year
Group Icon

Joined: 20 Mar, 2008
Posts: 1,274



Thanked: 58 times
Dream Kudos: 325
My Contributions
Well, you could use either of the examples that I will provide.

CODE
for (int x = 0; x <= 10; x++)
     System.out.println(count[x]);



Or, you could use this next one, which is a more leniant way of doing it...except you have to have JDK 5 or later I think, best to be safe by saying 6 though.

CODE
for (int i : count)  // count is the name of your array, I just used your name
     System.out.println(i);


That is the for-each syntax, and I'm not experienced with it much, so it may be wrong. However, I am SURE about the first one.

This post has been edited by Locke37: 2 Apr, 2008 - 05:14 PM
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Displaying An Array
2 Apr, 2008 - 05:11 PM
Post #3

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
Just a suggestion, but you can reference an index of the array with a variable, that saves you from long switch blocks.
CODE
int num = (int) (Math.random() * 10);
count[num]++;


And do what locke37 said to print out the array. and you can use a foreach loop in java5
User is online!Profile CardPM
+Quote Post

baavgai
RE: Displaying An Array
2 Apr, 2008 - 05:15 PM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,291



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
This is kind of a gimme; it can't be explained without offering it. But, before we get there; that case statement!

Consider:
CODE

switch(num){
    case 2:
        count[2]++;
        break;


Your num in the case is the same as the array index?

Minor rewrite:
java

for(int i = 0; i < 10; i++){
int num = (int)Math.random() * 10;
count[num]++;
}


How to print an array? There are a number of ways, but basically you want to interate through the array and show each element.

java

for(int i = 0; i < count.length; i++){
System.out.println(count[i]);
}


Hope this helps.

EDIT: man, when I hit reply there were no responses. Um, yeah, what everyone else said. tongue.gif


This post has been edited by baavgai: 2 Apr, 2008 - 05:17 PM
User is online!Profile CardPM
+Quote Post

Runesmith
RE: Displaying An Array
2 Apr, 2008 - 07:44 PM
Post #5

New D.I.C Head
*

Joined: 7 Mar, 2008
Posts: 15

I tried Locke's solution, but I keep getting an "exception" error a second or so after I try to run it. No errors are popping up on my compiler, though. Here's the code I'm using:
CODE

package array;
/**
*
* @author Runesmith
*/
public class Main {

    
    public static void main(String[] args) {
        int[] count = new int[10];
        int num;
        for(int i = 0; i < 10; i++){
            num = (int)Math.random() * 10;
            switch(num){
                case 0:
                    count[0]++;
                    break;
                case 1:
                    count[1]++;
                    break;
                case 2:
                    count[2]++;
                    break;
                case 3:
                    count[3]++;
                    break;
                case 4:
                    count[4]++;
                    break;
                case 5:
                    count[5]++;
                    break;
                case 6:
                    count[6]++;
                    break;
                case 7:
                    count[7]++;
                    break;
                case 8:
                    count[8]++;
                    break;
                case 9:
                    count[9]++;
                    break;
                case 10:
                    count[10]++;
                    break;
                
              
              
            }
            for (int x = 0; x <= 10; x++)
            System.out.println(count[x]);
        }
    }
}

I also tried to use the other code segments, but none of them worked at all. I'm probably just putting the code in the wrong spot, though...

This post has been edited by Runesmith: 2 Apr, 2008 - 07:45 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Displaying An Array
2 Apr, 2008 - 08:19 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(Runesmith @ 2 Apr, 2008 - 08:44 PM) *

I tried Locke's solution, but I keep getting an "exception" error a second or so after I try to run it. No errors are popping up on my compiler, though. Here's the code I'm using:
CODE

package array;
/**
*
* @author Runesmith
*/
public class Main {

    
    public static void main(String[] args) {
        int[] count = new int[10];
        int num;
        for(int i = 0; i < 10; i++){
            num = (int)Math.random() * 10;
            switch(num){
                case 0:
                    count[0]++;
                    break;
                case 1:
                    count[1]++;
                    break;
.........
                case 8:
                    count[8]++;
                    break;
                case 9:
                    count[9]++;
                    break;
                case 10:
                    count[10]++;
                    break;
                
              
              
            }
            for (int x = 0; x <= 10; x++)
            System.out.println(count[x]);
        }
    }
}

I also tried to use the other code segments, but none of them worked at all. I'm probably just putting the code in the wrong spot, though...


Your variable count is int count[10]
So there are count[0], count[1], .... count[8], count[9]

doing

CODE

for (int x = 0; x <= 10; x++)
    System.out.println(count[x]);



will try to access count[10]

you should do

CODE

for (int x = 0; x < 10; x++)
    System.out.println(count[x]);


or even better

CODE

for (int x = 0; x < count.length; x++)
    System.out.println(count[x]);


another problem is that Math.random() return a numeber betwwen 0.0 et 0.1
so you will have
0.000001, 0.0083, 0.00765, ......
multiplying the numbers by 10 will still get you 0 and count[0] will have a count of probably 10

Better to use a Random object that will return and int between 0 and the parameter:
(and as prevoiusly suggested get ride of the useless switch statement what will you do if you want numbers between 0 and 1000 ?)
so:

java

public class Main {

public static void main(String[] args) {
int[] count = new int[10];
int num;
Random random = new Random();
for(int i = 0; i < 10; i++){
// get random number betwween 0 (inclusive) and 10 (exclusive)
num = random.nextInt(10);
count[num]++;
}
for (int x = 0; x < 10; x++)
System.out.println(x + ") " + count[x]);



This post has been edited by pbl: 3 Apr, 2008 - 03:23 AM
User is offlineProfile CardPM
+Quote Post

hallizh
RE: Displaying An Array
2 Apr, 2008 - 08:23 PM
Post #7

New D.I.C Head
*

Joined: 9 Mar, 2008
Posts: 37


My Contributions
Gosh, just replied but my post didnt get to its place wink2.gif My bad ofc ...

0 1 2 3 4 5 6 7 8 9 10 ... thats 11 diffrent numbers ...

java

int[] count = new int[10];

This creates a array with 10 'slots' .. so you are basically missing one slot ... ohmy.gif

java

int[] count = new int[11];

Thats a working code, im sure u know why smile.gif

The exception says, "ArrayIndexOutOfBoundsException" which basically means the above smile.gif


EDIT: Guess someone beat me to it, but my post handles the exception though smile.gif

This post has been edited by hallizh: 2 Apr, 2008 - 08:28 PM
User is offlineProfile CardPM
+Quote Post

Locke37
RE: Displaying An Array
3 Apr, 2008 - 01:52 PM
Post #8

Contributor of the Year
Group Icon

Joined: 20 Mar, 2008
Posts: 1,274



Thanked: 58 times
Dream Kudos: 325
My Contributions
Ah, I didn't catch that, I just saw that the count[] variables went to [10], and went from there, but yes, that is an ArrayOutOfBoundsException. Simply increase the size of the array.
User is offlineProfile CardPM
+Quote Post

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

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