Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Displaying Array Not Working.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Displaying Array Not Working.

    The assignment is to write a program that will read a set of numbers, sort the numbers, and print the array values. My issue is that everything works just fine until it comes to printing the array values.

    package program11;
    import javax.swing.JOptionPane;
     
    public class Program11 {
        public static void main(String[] args) {
            double [] list = new double [10];
            getList(list);
            sort(list);
            printList(list);
        }
        public static void getList(double [] theList){
            for (int n = 0; n < theList.length; n++){
                theList [n] = Double.parseDouble(JOptionPane.showInputDialog(null,
                        "Enter a number:"));
            }
        }
        public static void sort(double [] theList){
            double hold;
            for (int a = 0; a < theList.length - 1; a++){
                for (int z = a + 1; z < theList.length; z++){
                    if (theList [a] > theList [z]){
                        hold = theList [a];
                        theList [a] = theList [z];
                        theList [z] = hold;
                    }
                }
            }
        }
        public static void printList(double [] list){
            JOptionPane.showMessageDialog(null, "The numbers are: " + (list));
        }
    }

    I know there's a problem with the printList method, but I'm not sure what exactly I need to do to get the program to display the array.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,252
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Displaying Array Not Working.

    What do you expect to happen? What happens instead?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Displaying Array Not Working.

    Try this...

    package program11;
    import javax.swing.JOptionPane;
     
    public class Program11 {
        public static void main(String[] args) {
            double [] list = new double [10];
            getList(list);
            sort(list);
            printList();
        }
        public static void getList(double [] theList){
            for (int n = 0; n < theList.length; n++){
                theList [n] = Double.parseDouble(JOptionPane.showInputDialog(null,
                        "Enter a number:"));
            }
        }
        public static void sort(double [] theList){
            double hold;
            for (int a = 0; a < theList.length - 1; a++){
                for (int z = a + 1; z < theList.length; z++){
                    if (theList [a] > theList [z]){
                        hold = theList [a];
                        theList [a] = theList [z];
                        theList [z] = hold;
                    }
                }
            }
        }
        public static void printList(){
    JFrame frame  = new JFrame();
    JTextArea text = new JTextArea();
    frame.add(text);
            for(int i = 0; i <= 10; i++){
                text.append(list[i] + ", ");
         }
    frame.setVisible(true);
        }
    }
    Last edited by macko; November 3rd, 2011 at 10:02 AM.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Displaying Array Not Working.

    As I said, the program doesn't display the array as it's supposed to, but instead displays a jumbled code (e.g., [D@1e536d6). I type in numbers then the program is supposed to sort the array into ascending order and display the sorted array.

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Displaying Array Not Working.

    Have you tried my code update?

  6. #6
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Displaying Array Not Working.

    Give me a second..

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,252
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Displaying Array Not Working.

    Quote Originally Posted by tleblanc View Post
    @macko,

    Is there a way to do it in JOptionPane?
    What have you tried??
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Displaying Array Not Working.

    DELETED...
    Last edited by macko; November 3rd, 2011 at 10:07 AM.

  9. #9
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Displaying Array Not Working.

    DELETED, Reason: Do not want to display full answers to your problems, Please ask for help step by step to work through it...
    Last edited by macko; November 3rd, 2011 at 10:42 AM.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,252
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Displaying Array Not Working.

    Recommended reading: The Problem with Spoon-feeding
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Displaying Array Not Working.

    lol lovin that topic title bahahahaha the problem with spoon-feeding

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,252
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Displaying Array Not Working.

    Quote Originally Posted by macko View Post
    lol lovin that topic title bahahahaha the problem with spoon-feeding
    Well, it was meant for you. I appreciate you trying to help, but what happens next time the OP has a test on this stuff? Giving out full code solutions might help in the short term, but in the long term, it's better to ask questions to help people work through their own problems. It's easy to get caught up in trying to solve a person's problems, but it's much more helpful to help them solve their own problems.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Displaying Array Not Working.

    ahh.. sorry man.. i wont give out any more solutions then

Similar Threads

  1. Displaying the other
    By Twoacross in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2011, 08:58 AM
  2. Not displaying
    By toksiks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 07:32 AM
  3. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 11:40 AM
  4. Not Displaying what I need
    By rob3097 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2010, 09:31 PM
  5. Replies: 4
    Last Post: January 26th, 2009, 11:03 PM