Results 1 to 9 of 9

Thread: Help with Arrays

  1. #1
    Join Date
    Nov 2005
    Posts
    2,008
    Rep Power
    0

    Exclamation Help with Arrays

    Okay I am making this program and I have everything down except the last part to display the results. Here's what I have so far. THe error (in red) is that it can't find symbol : JOptionPane

    The program generates 100 random integers between 0 and 9 and should display the # of each occurence.

    Code:
    public class CountingSingleDigits {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // Declare and create and array
            int[] ints = createArray();
            
            // Count occurence of each number
            int[] counts = countLetters(ints);
            
            // Call display result
            displayCounts(counts);
        } 
        public static int[] createArray() {
            int[] ints = new int[100]; //make new array to hold 100 ints.
             
            for (int i = 0; i < ints.length; i++)
                    ints[i] = (int)(Math.random() * 10); //generate 100 random ints
            return ints;                                 //between 0 and 9
        }
            
        public static int[] countLetters(int[] ints) {
            int[] counts = new int[10]; //make new array to store occurences
                    
            for (int i = 0; i < ints.length; i++) //count how many occurences
                        counts[ints[i] - 0]++ ;
            return counts;
        }
        
        // Display results
        public static void displayCounts(int[] counts) {
            for (int i = 0; i< counts.length; i++) 
               
                JOptionPane.showMessageDialog(counts[i] + (i + 0) + "\n" 
                       , JOptionPane.INFORMATION_MESSAGE); 
            
        }    
        
    }
    [AMD Gallardo] [Asus M2N SLI Deluxe] [AMD 5000+ X2 black edition @ 3.2GHZ] [Zalman CNPS9700 LED][ EVGA 8800GT 512MB] [Gskill 2GB x 2 DDR2800] [Seagate 320GB IDE][Seagate 1TB, 750GB, 320GB SATA II][Western Digital 1TB SATA II][XION 600W] [NEC DL DVD-RW] [Antec Nine Hundred MidATX] [19" BenQ FP93GX LCD][40" Samsung LNA500LCD HDTV 1080p][Logitech X-230 2.1]

  2. #2
    Join Date
    Sep 2005
    Posts
    55
    Rep Power
    0

    Default

    You need to import JOptionPane!

    Start your code with

    Code:
    import javax.swing.JOptionPane;

  3. #3
    Join Date
    Sep 2005
    Posts
    55
    Rep Power
    0

    Default

    And not to say that your code will be working then - to output something with Swing you will need quite a bit more code ...

    I'd advise to work through a Swing tutorial.

  4. #4
    Join Date
    Nov 2005
    Posts
    2,008
    Rep Power
    0

    Default

    Doh...I knew it was something stupid...but yea,

    showMessageDialog is what is highlighted now with this error

    symbol : method showMessageDialog(java.lang.String,int)
    location: class javax.swing.JOptionPane
    JOptionPane.showMessageDialog(counts[i] + (i + 0) + "\n"
    1 error

    Hmm, Not sure...
    [AMD Gallardo] [Asus M2N SLI Deluxe] [AMD 5000+ X2 black edition @ 3.2GHZ] [Zalman CNPS9700 LED][ EVGA 8800GT 512MB] [Gskill 2GB x 2 DDR2800] [Seagate 320GB IDE][Seagate 1TB, 750GB, 320GB SATA II][Western Digital 1TB SATA II][XION 600W] [NEC DL DVD-RW] [Antec Nine Hundred MidATX] [19" BenQ FP93GX LCD][40" Samsung LNA500LCD HDTV 1080p][Logitech X-230 2.1]

  5. #5
    Join Date
    Sep 2005
    Posts
    55
    Rep Power
    0

    Default

    Quote Originally Posted by Genesis2kx View Post
    Doh...I knew it was something stupid...but yea,

    showMessageDialog is what is highlighted now with this error

    symbol : method showMessageDialog(java.lang.String,int)
    location: class javax.swing.JOptionPane
    JOptionPane.showMessageDialog(counts[i] + (i + 0) + "\n"
    1 error

    Hmm, Not sure...
    As I said, I strongly recommend working through a Swing tutorial. At least you should have the Javadocs at hand - you'd know why your approach can't work.

    Do you really need a graphical output for your program?

    Why not just use a simple output to the console, like:

    Code:
    System.out.println(counts[i]+" "+i);
    and all is well ... ;-)

    Michael

  6. #6
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Code:
    JOptionPane.showMessageDialog("value " + i + ", occurs " + counts[i] + " time(s)", JOptionPane.INFORMATION_MESSAGE);
    you might also want to consider concatenating all those counts into a single string inside the loop and then displaying it once, rather than a billion popups.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  7. #7
    Join Date
    Nov 2005
    Posts
    2,008
    Rep Power
    0

    Default

    Thanks codex
    Hmm, Yea i decided to just go with the println method...but....hmm your right icymint. That's actually how i wanted the output but so far i have only worked with results that show at once and didn't even realize it would pop up for each result...I'll check that way later.
    [AMD Gallardo] [Asus M2N SLI Deluxe] [AMD 5000+ X2 black edition @ 3.2GHZ] [Zalman CNPS9700 LED][ EVGA 8800GT 512MB] [Gskill 2GB x 2 DDR2800] [Seagate 320GB IDE][Seagate 1TB, 750GB, 320GB SATA II][Western Digital 1TB SATA II][XION 600W] [NEC DL DVD-RW] [Antec Nine Hundred MidATX] [19" BenQ FP93GX LCD][40" Samsung LNA500LCD HDTV 1080p][Logitech X-230 2.1]

  8. #8
    Join Date
    Apr 2008
    Posts
    11
    Rep Power
    0

    Default

    I hope you are using netBeans or Eclipse as IDE. NetBeans, my favorite will tell you if you missed an import and even add the line for you.

    If you write a variable all occurences will be highlighted, if you send an object a message the parameters and their type will automaticall appear in a bubble etc. (Like Visual Studio but for an open Programming language)

    And best of all: It's free.

    See and download: http://www.netbeans.org/
    or: http://www.eclipse.org/
    Last edited by gstrubinsky; Apr 18, 2008 at 02:21 PM.

  9. #9
    Join Date
    Nov 2005
    Posts
    2,008
    Rep Power
    0

    Default

    its funny. I am using netbeans 6.0 It does help out in some cases but in that case it didn't show that the import was missing. Usually though if I import and it isn't used it tells me but not the other way.. I believe I have all settings on default so I'm not sure if i need to turn it on or something.
    [AMD Gallardo] [Asus M2N SLI Deluxe] [AMD 5000+ X2 black edition @ 3.2GHZ] [Zalman CNPS9700 LED][ EVGA 8800GT 512MB] [Gskill 2GB x 2 DDR2800] [Seagate 320GB IDE][Seagate 1TB, 750GB, 320GB SATA II][Western Digital 1TB SATA II][XION 600W] [NEC DL DVD-RW] [Antec Nine Hundred MidATX] [19" BenQ FP93GX LCD][40" Samsung LNA500LCD HDTV 1080p][Logitech X-230 2.1]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •