i need help with a project.
Is it possible to display an 1 dimension array in GUI without using a loop?
If it is can you send me the codes?

i was ask to enter the numbers like thise

Example:

Enter numbers: 3, 4, 1, 5, 6

then it should display the numbers which i entered.

Example:

Number entered: 3, 4, 1, 5, 6

next i have have to sort the numbers (which i already know how).
it should display in a single GUI box.

Example:

Sorted numbers are: 6, 5, 4, 3, 1

can you please help me?

Why can't you use a loop to do this? It would be so much easier and it really shouldn't matter if you do.

If it is that important not to use a loop, do you know the size of the array before the user inputs the numbers?

I suppose a recursive method call would do the trick, but you really should be using loops for this, which you'll need anyway to sort the numbers.

Well this is the code that i made I'm not done with sorting yet my only problem is the looping output is their any way not to loop my output

import javax.swing.*;
public class SortDemo
{
public static void main(String[] args)
{


String temp1,temp2;


temp1 = JOptionPane.showInputDialog("How many numbers do you want to sort?");
int num;
num = Integer.parseInt(temp1);
int arr [] = new int [num];


for (int x = 0;x<num;x++)
{
temp2 = JOptionPane.showInputDialog("Enter array Elements" );
arr[x] = Integer.parseInt(temp2);
}


// thise is the output part


for (int y=0;y<num;y++)
{
JOptionPane.showMessageDialog(null, arr[y]);
}



}
}

Hi Loreto,

Since I assume you have tried your best here is my help.
If you are serious about programming please don't just do copy and paste work instead Understand and use this snippet.

import javax.swing.*;
public class SortDemo
{
	static int arr [] ;
	static int i;
        public static void main(String[] args)
	{
           // Get the size as you posted
           // Accept the user inputs
		display();
	}

	public static void display() {
		if(i<arr.length) {
			JOptionPane.showMessageDialog(null, arr[i]);
			i++;
		}
		else
			System.exit(0);
		display();
	}
}

Hope this helps

Hi Loreto,

What I think you are trying to do is have one JOptionPane print out all the numbers once but your code is printing each number one at a time in separate JOptionPanes. So what you need to do is create a String with the numbers you want to print in the loop you have, then outside your loop call the JOptionPane with the String rather than arr[y].

Hope this points you in the right direction :)

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.