Not so new anymore more with Java but as it gets more advanced (for me that at least) the tricks evade me. I would like to output sorted values into my GUI but two things are not happening.

1. Cannot get values to print in GUI but prints on screen OK.
(note line 26)
2. Cannot get sorted random values but sorts manual inputs OK.
(note line 23)

import java.awt.*;         // Supplies layout manager
   import java.awt.event.*;   // Supplies event classes
   import javax.swing.*;      // Supplies class JApplet
   import java.util.*;        // Supplies class Scanner
   import java.applet.*;	   // Supplies class Applet

   public class RandomIntegers extends JApplet implements ActionListener {
   
      public void actionPerformed(ActionEvent event) {
      // Event handler
         int value;
         value = Integer.parseInt(inputField.getText());
         inputField.setText("");
         //outLabel.setText("List size = " + value + "\n values sorted are: ");
      	
      	// Generate random integers and apply sort method
         Random randomGenerator = new Random();
         for (int i = 1; i <= 10; ++i) {
            int randomInt = randomGenerator.nextInt(value);
            // Assign random list to array
            int listItems[] = { randomInt };
         	// Manual value assignment sorts as expected
         	//int listItems[] = {23,27,74,2,99,69,94,18,83,10};
            sorting(listItems, listItems.length);
            for (int index = 0; index < listItems.length; index++)
               System.out.print(listItems[index] + " "); // need these results in GUI
            outLabel.setText("List size = " + value);
				outLabel2.setText("The values sorted are: ");
         }			
      }
   
   // The sorting method process
      public static void sorting( int array[], int length ) {
         int index = 0;       // Index through array
         int counter = 0;     // Smaller value
         int temp = 0;        // Temp position
      // Loop through each element of the array
         for(index = 0; index < length; index++) {
         // Once for each element minus the counter
            for(counter = 1; counter < (length-index); counter++) {
            // Test if need to swap or not
               if(array[counter-1] > array[counter]) {
               // Next three lines just swap the two elements
                  temp = array[counter-1];
                  array[counter-1] = array[counter];
                  array[counter] = temp;
               }
            }
         }
      }
   
   // Set up a button, label, and input field
      private JTextField inputField;
      private JLabel label;
      private JLabel outLabel;
		private JLabel outLabel2;
      private JButton button;
   
      public void init() {
      // Instantiate the GUI components    
         JLabel label;
         label = new JLabel ("Enter value:");
         outLabel = new JLabel ("List size");
			outLabel2 = new JLabel ("Sorted values");
         inputField = new JTextField("0");
      
         JButton button;
         button = new JButton("Enter");
         button.addActionListener(this);
      
         add(label);
         add(inputField);
         add(button);
         add(outLabel);
			add(outLabel2);
         // Specify layout manager
         setLayout(new GridLayout(0,1));
      }
   }

Line21: in your case each time listItems.length==1.
then
declare int listItems[] outside for-loop(line18), with size=10,
use for-loop i-index for fill listItems
end of this loop before line24

Line21: in your case each time listItems.length==1.
then
declare int listItems[] outside for-loop(line18), with size=10,
use for-loop i-index for fill listItems
end of this loop before line24

Thank you for your suggestion but I truly do not understand your recommendation.

After generating random values stipulated by user (eg. 100), the code for loop picks the first ten and sorts them. I gather I need to place those ten numbers in the array in order to sort them.

public void actionPerformed(ActionEvent event) {
      // Event handler
         int value;
         int randomInt = 0;
         value = Integer.parseInt(inputField.getText());
         inputField.setText("");
      	// Generate random integers and apply sort method
         Random randomGenerator = new Random();
         for (int i = 1; i <= 10; ++i) {
            randomInt = randomGenerator.nextInt(value);
         }
            // Assign random list to array
         int listItems[] = { randomInt }; // This in theory should be like line #15
         	// Manual value assignment sorts as expected
         	//int listItems[] = {23,27,74,2,99,69,94,18,83,10};
         sorting(listItems, listItems.length);
         for (int index = 0; index < listItems.length; index++)
            System.out.print(listItems[index] + " "); // need these results in GUI
         outLabel.setText("List size = " + value);
         outLabel2.setText("The values sorted are: ");
      }

Thanks,
Jems

once more
line10: each time you assigned random value to varible randomInt
each next assignation destroyed the previous value
randomInt "remembers" only last value
randomInt is not an array

To rapair this declare listItems as array of int values with size=10 and do this before line9

int[] listItems = new int[10];

Inside loop (line10) assign

listItems[i-1] = randomGenerator.nextInt(value);

Why index is [i-1] -because you started index from 1 (not from zero)
You can change it.

once more
line10: each time you assigned random value to varible randomInt
each next assignation destroyed the previous value
randomInt "remembers" only last value
randomInt is not an array

To rapair this declare listItems as array of int values with size=10 and do this before line9

int[] listItems = new int[10];

Inside loop (line10) assign

listItems[i-1] = randomGenerator.nextInt(value);

Why index is [i-1] -because you started index from 1 (not from zero)
You can change it.

Thank you very much, not only have I learned my error but also became clear on program convention of declaring my variable and array outside of a loop. I understand where you have pointed out my errors and I see where it was my fault treating the variable randomInt as and array. Based on the clarification I do not even need this variable.

Now that I printed the result as expected onscreen (using System.out.print(....), I need to have these results output in my GUI. Using listItems[index] + " ") does not do the trick. It would appear the output conventions is not that simple because I get an error "cannot find symbol" at line 20.

public void actionPerformed(ActionEvent event) {
      // Event handler
         int value;          // user input random size
         int len = 10;       // set number of values to sort
         value = Integer.parseInt(inputField.getText());
         inputField.setText("");
      	// Generate random integers and apply sort method
         Random randomGenerator = new Random();
         int listItems[] = new int[len];
         for (int i = 0; i < len; ++i) {
            // Assign random list to array
            listItems[i] = randomGenerator.nextInt(value);
         }
      // Manual value assignment sorts as expected
      //int listItems[] = {23,27,74,2,99,69,94,18,83,10};
         sorting(listItems, listItems.length);
         for (int index = 0; index < listItems.length; index++)
            System.out.print(listItems[index] + " "); // need these results in GUI
         outLabel.setText("List size = " + value);
         outLabel2.setText("The values sorted are: " + listItems[index] + " "); // I suppose I am using this statement incorrectly
      }

line17: body of your for-loop is only line18
use bracket { ... } to determine the extent of the for-loop

line17: body of your for-loop is only line18
use bracket { ... } to determine the extent of the for-loop

Thank you for getting me thinking outside the box. Here is what I did based on what you are recommending. I created a variable and concatenated the results and it worked well - problem solved.

String result = ""; // Holds output values
result += listItems[index] + " "; // Assigns array values to holder variable
outLabel2.setText("The values sorted are: " + result); // Spits out result as expected
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.