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.
Code :
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.
Re: Displaying Array Not Working.
What do you expect to happen? What happens instead?
Re: Displaying Array Not Working.
Try this...
Code :
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);
}
}
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.
Re: Displaying Array Not Working.
Have you tried my code update?
Re: Displaying Array Not Working.
Re: Displaying Array Not Working.
Quote:
Originally Posted by
tleblanc
@macko,
Is there a way to do it in JOptionPane?
What have you tried??
Re: Displaying Array Not Working.
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...
Re: Displaying Array Not Working.
Re: Displaying Array Not Working.
lol :P lovin that topic title bahahahaha the problem with spoon-feeding
Re: Displaying Array Not Working.
Quote:
Originally Posted by
macko
lol :P 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.
Re: Displaying Array Not Working.
ahh.. sorry man.. i wont give out any more solutions then :D