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 11 of 11

Thread: Need a little help with the basics

  1. #1
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Need a little help with the basics

    Hi all...I have this program here

       import javax.swing.JOptionPane;
       public class BubbleSort {      
          public static void main(String[] args) {
             double[] list = new double[10]; 
             bubbleSort(list); 
    			JOptionPane.showMessageDialog(null, "Thanks for playing!");    
          }
        //The Method for sorting the numbers
          public static void bubbleSort(double[] list) {
           //Declare variables
             double currentMin;
             int currentMinIndex, i, j;
           //Store user input
             for (i = 0; i < list.length; i++) 
                list[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter next numbers."));
    		 //Print the numbers entered		
             JOptionPane.showMessageDialog(null, "The numbers you entered were: \n" + list[0] + " " + list[1] + " " + list[2] + " " + 
                list[3] + " " + list[4] + " " + list[5] + " " + list[6] + " " + list[7] + " " + list[8] + " " + list[9]);
     
             JOptionPane.showMessageDialog(null, "The numbers will now swap till they are sorted from low to high: ");
     
             for (i = 0; i < list.length - 1; i++) {
              //Find the minimum in the list 
                currentMin = list[i];
                currentMinIndex = i;
     
                for (j = i + 1; j < list.length; j++) {
                   if (currentMin > list[j]) {
                      currentMin = list[j];
                      currentMinIndex = j;
                   }
                }
              //Swap list[i] with list[currentMinIndex] if necessary
                if (currentMinIndex != i) {
                   list[currentMinIndex] = list[i];
                   list[i] = currentMin;
                   //Print the sorted numbers                
                   JOptionPane.showMessageDialog(null, list[0] + " " + list[1] + " " + list[2] + " " + list[3] + " " + list[4]
                      + " " + list[5] + " " + list[6] + " " + list[7] + " " + list[8] + " " + list[9]);  
                }
             }
          }
       }

    While my teacher gave this assignment an A, he wants me to get rid of this type of code...

             JOptionPane.showMessageDialog(null, "The numbers you entered were: \n" + list[0] + " " + list[1] + " " + list[2] + " " + 
                list[3] + " " + list[4] + " " + list[5] + " " + list[6] + " " + list[7] + " " + list[8] + " " + list[9]);

    He hinted at the += style using a string. Does anyone know what he's talking about?

    Neil


  2. #2
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Need a little help with the basics

    He most likely meant to use a loop to set up the string:

     
    String result = "The numbers you......";
    for (int i = 0; i < list.length; i++) {
      result += numbers.....
    }
    Last edited by vanDarg; March 17th, 2011 at 07:28 PM.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  3. The Following User Says Thank You to vanDarg For This Useful Post:

    Java Neil (March 18th, 2011)

  4. #3
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need a little help with the basics

    I'm not quite sure what you mean. Doing it that way would produce the numbers one at a time for each loop wouldn't it? He wants the numbers entered to be displayed in one window.

    Of coarse I could have completely misunderstood you as well. For some reason your code was not displayed right.

    Thanks for your help either way! I love this place.

  5. #4
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Need a little help with the basics

    The second parameter of the method showMessageDialog() takes a String. Therefore, you can send it a String, or a String variable:

    JOptionPane.showMessageDialog(null, "");
      // or
    JOptionPane.showMessageDialog(null, variableOfTypeString);

    So, you would need to setup the String variable. What your professor was hinting at, (or at least the way I would do it) was to loop through your array of doubles, and enter them into a string. Think of it this way:

    String result = "The numbers you entered were: ";
    String = String + list[0] + " ";            // The empty space is for separation between your different elements in the array
       // if the value of list[0] was 13.4, then your string would now be "The numbers you entered were: 13.4 "
       // You can also do it this way:
    String += list[0] + " ";   
     
      // And ultimately, loop though it so you don't have extra lines of code:
    String result = "The numbers you......";
    for (int i = 0; i < list.length; i++) {
      result += list[i];
    }

    And use the string you created:

    JOptionPane.showMessageDialog(null, result);
    Hope this helps
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  6. #5
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need a little help with the basics

    Well I can't thank you enough for your help. Unfortunately I'm still missing something.

    Here is your suggestions implemented in my code...

        import javax.swing.JOptionPane;
       public class BubbleSort {      
          public static void main(String[] args) {
             double[] list = new double[10]; 
             bubbleSort(list); 
             JOptionPane.showMessageDialog(null, "Thanks for playing!");    
          }
        //The Method for sorting the numbers
          public static void bubbleSort(double[] list) {
           //Declare variables
             double currentMin;
             int currentMinIndex, i, j;
           //Store user input
             for (i = 0; i < list.length; i++) 
                list[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter next numbers."));
           //Print the numbers entered		
             String result = "The numbers you entered were: ";
             for (i = 0; i < list.length; i++) {
                result += list[i];
             }
     
             JOptionPane.showMessageDialog(null, "The numbers will now swap till they are sorted from low to high: ");
     
             for (i = 0; i < list.length - 1; i++) {
              //Find the minimum in the list 
                currentMin = list[i];
                currentMinIndex = i;
     
                for (j = i + 1; j < list.length; j++) {
                   if (currentMin > list[j]) {
                      currentMin = list[j];
                      currentMinIndex = j;
                   }
                }
              //Swap list[i] with list[currentMinIndex] if necessary
                if (currentMinIndex != i) {
                   list[currentMinIndex] = list[i];
                   list[i] = currentMin;
                   //Print the sorted numbers                
                   JOptionPane.showMessageDialog(null, list[0] + " " + list[1] + " " + list[2] + " " + list[3] + " " + list[4]
                      + " " + list[5] + " " + list[6] + " " + list[7] + " " + list[8] + " " + list[9]);  
                }
             }
          }
       }

    As you can see I'm missing something?

  7. #6
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Need a little help with the basics

    Not sure exactly what needs further explanation, but for instance, you are creating the String variable, but are not sending it to the method to display it. Also, remember to add a space in your String between each number.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  8. #7
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need a little help with the basics

    Quote Originally Posted by vanDarg View Post
    Not sure exactly what needs further explanation, but for instance, you are creating the String variable, but are not sending it to the method to display it. Also, remember to add a space in your String between each number.
    Sorry for not following you.
    I guess I a bit confused by this...

    String result = "The numbers you entered were: ";
    This is not using JOptionPane for some reason?

    I'm just not seeing between the lines.

    Maybe you could apply your suggestion to my original code so I could see the whole picture? Like I said, I already have the grade for the assignment, and am just trying to approach things in a more efficient way.

    Neil

  9. #8
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Need a little help with the basics

    No worries! You DO want to use the String variable with JOptionPane.

    I was just playing around with it in eclipse, see if this bit helps:

    String result = "This is my text, stored in a String variable";
    JOptionPane.showMessageDialog(null, "This is my text...");
    JOptionPane.showMessageDialog(null, result);

    Sending a String variable to the method, will display the contents of the variable in the "pane" that is displayed. In this instance, sending the String variable 'result' to the method showMessageDialog would display (in a message box, of course):
    This is my text, stored in a String variable
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  10. #9
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need a little help with the basics

    I just feel like I'm spinning my wheels here.

    Can you apply your knowledge to this code so I can "see" the whole thing? I think I'm just missing how the code looks as a whole. Once I see it in practice I can trace the logic.

       import javax.swing.JOptionPane;
       public class SelectionSort {
          public static void main(String[] args) {
             double[] list = new double[10];
             selectionSort(list); 
          }
       //The method for sorting the numbers
          public static void selectionSort(double[] list) {
             for (int i = 0; i < list.length - 1; i++) { 
             //Find the minimum int the list[..list.length - 1]
                double currentMin = list[i];
                int currentMinIndex = i;
     
                for (int j = i + 1; j < list.length; j++) {
                   if (currentMin > list[j]) {
                      currentMin = list[j];
                      currentMinIndex = j;
                   }
                }
     
             //Swap list[i] with list[currentMinIndex] if nessasary
                if (currentMinIndex != i) {
                   list[currentMinIndex] = list[i];
                   list[i] = currentMin;
                }           
             }
          }
       }

    How long have you been doing this, by the way?

  11. #10
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Need a little help with the basics

    A quick example to show what I was getting at, the code I added is between the commented lines
       import javax.swing.JOptionPane;
       public class SelectionSort {
          public static void main(String[] args) {
             double[] list = new double[10];
             selectionSort(list); 
          }
       //The method for sorting the numbers
          public static void selectionSort(double[] list) {
             for (int i = 0; i < list.length - 1; i++) { 
             //Find the minimum int the list[..list.length - 1]
                double currentMin = list[i];
                int currentMinIndex = i;
     //--------------------------------------------------------------------------------------           
                //Store user input
                for (i = 0; i < list.length; i++) 
                   list[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter next numbers."));
     
                String result = "Numbers are: ";
                for (i = 0; i < list.length; i++) {
                	result += list[i] + " ";
                }
     
                JOptionPane.showMessageDialog(null, result);
    //-----------------------------------------------------------------------------------------            
                for (int j = i + 1; j < list.length; j++) {
                   if (currentMin > list[j]) {
                      currentMin = list[j];
                      currentMinIndex = j;
                   }
                }
     
             //Swap list[i] with list[currentMinIndex] if nessasary
                if (currentMinIndex != i) {
                   list[currentMinIndex] = list[i];
                   list[i] = currentMin;
                }           
             }
          }
       }

    What I was saying should hopefully make some sense now. I have been programming for roughly 10 years, on and off. Currently a CS student.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  12. #11
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need a little help with the basics

    What I was saying should hopefully make some sense now. I have been programming for roughly 10 years, on and off. Currently a CS student.
    It really shows. I can't thank you enough!

    I'm going to mess around with this logic for a while and post back.