Confused about Arrays and JOptionPane!
Hi all...I have just started the chapter on Arrays today and I'm a bit spun. Fistly My teach expects everything that can be done in JOptionPane to be done that way.
Here is my assignment code so you can see how bad off I am.
Code java:
import javax.swing.JOptionPane;
import java.util.*;
public class SmallestElement {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
double[] list = new double[10];
double sum, biggestnumber, smallestnumber;
int index;
//Store user input
JOptionPane.showMessageDialog(null, "Enter ten integers: ");
list[0] = Integer.parseInt(JOptionPane.showInputDialog(null, "1st number" + " "));
list[1] = Integer.parseInt(JOptionPane.showInputDialog(null, "2nd number" + " "));
list[2] = Integer.parseInt(JOptionPane.showInputDialog(null, "3rd number" + " "));
list[3] = Integer.parseInt(JOptionPane.showInputDialog(null, "4th number" + " "));
list[4] = Integer.parseInt(JOptionPane.showInputDialog(null, "5th number" + " "));
list[5] = Integer.parseInt(JOptionPane.showInputDialog(null, "6th number" + " "));
list[6] = Integer.parseInt(JOptionPane.showInputDialog(null, "7th number" + " "));
list[7] = Integer.parseInt(JOptionPane.showInputDialog(null, "8th number" + " "));
list[8] = Integer.parseInt(JOptionPane.showInputDialog(null, "9th number" + " "));
list[9] = Integer.parseInt(JOptionPane.showInputDialog(null, "10th number" + " "));
//Sum the input
sum = list[0] + list[1] + list[2] + list[3] + list[4] + list[5] + list[6] + list[7] + list[8] + list[9];
JOptionPane.showMessageDialog(null, "The sum of the numbers = " + sum);
//Print in reverse order
JOptionPane.showMessageDialog(null, "The numbers in reverse order are: ");
JOptionPane.showMessageDialog(null, list[9] + " " + list[8] + " " + list[7] + " " + list[6] + " " + list[5]
+ " " + list[4] + " " + list[3] + " " + list[2] + " " + list[1] + " " + list[0]);
//Find the largest Number in the array
int maxIndex = 0;
for (index = 1; index < list.length; index++) {
if (list[maxIndex] < list[index])
maxIndex = index;
}
biggestnumber = list[maxIndex];
JOptionPane.showMessageDialog(null, "The largest number you entered was: " + biggestnumber);
//Find the Smallest Number in the array
int minIndex = 0;
for (index = 0; index < list.length; index++)
if (list[minIndex] > list[index])
minIndex = index;
smallestnumber = list[minIndex];
JOptionPane.showMessageDialog(null, "The smallest number you entered was: " + smallestnumber);
}
}
What I need is to create this program with as few a function handled by the main method and use a heading called
Code java:
public static min(double[] array)
for example...
Any advice would be great.
Neil
Re: Confused about Arrays and JOptionPane!
Hello Neil,
Take a look at this example.
I have taken the 'Find the Smallest Number in the array' part out of the main method and it is now in a method called min.
As you can see, you need to declare your variables outside of the main method.
I hope this helps you move forward.
Code Java:
import javax.swing.JOptionPane;
import java.util.*;
public class SmallestElement {
static Scanner console = new Scanner(System.in);
static int index;
static double[] list = new double[10];
static double sum, biggestnumber, smallestnumber;
public static void main(String[] args) {
//Store user input
JOptionPane.showMessageDialog(null, "Enter ten integers: ");
list[0] = Integer.parseInt(JOptionPane.showInputDialog(null, "1st number" + " "));
list[1] = Integer.parseInt(JOptionPane.showInputDialog(null, "2nd number" + " "));
list[2] = Integer.parseInt(JOptionPane.showInputDialog(null, "3rd number" + " "));
list[3] = Integer.parseInt(JOptionPane.showInputDialog(null, "4th number" + " "));
list[4] = Integer.parseInt(JOptionPane.showInputDialog(null, "5th number" + " "));
list[5] = Integer.parseInt(JOptionPane.showInputDialog(null, "6th number" + " "));
list[6] = Integer.parseInt(JOptionPane.showInputDialog(null, "7th number" + " "));
list[7] = Integer.parseInt(JOptionPane.showInputDialog(null, "8th number" + " "));
list[8] = Integer.parseInt(JOptionPane.showInputDialog(null, "9th number" + " "));
list[9] = Integer.parseInt(JOptionPane.showInputDialog(null, "10th number" + " "));
//Sum the input
sum = list[0] + list[1] + list[2] + list[3] + list[4] + list[5] + list[6] + list[7] + list[8] + list[9];
JOptionPane.showMessageDialog(null, "The sum of the numbers = " + sum);
//Print in reverse order
JOptionPane.showMessageDialog(null, "The numbers in reverse order are: ");
JOptionPane.showMessageDialog(null, list[9] + " " + list[8] + " " + list[7] + " " + list[6] + " " + list[5]
+ " " + list[4] + " " + list[3] + " " + list[2] + " " + list[1] + " " + list[0]);
//Find the largest Number in the array
int maxIndex = 0;
for (index = 1; index < list.length; index++) {
if (list[maxIndex] < list[index])
maxIndex = index;
}
biggestnumber = list[maxIndex];
JOptionPane.showMessageDialog(null, "The largest number you entered was: " + biggestnumber);
// call min method
min();
}
public static void min(){
//Find the Smallest Number in the array
int minIndex = 0;
for (index = 0; index < list.length; index++)
if (list[minIndex] > list[index])
minIndex = index;
smallestnumber = list[minIndex];
JOptionPane.showMessageDialog(null, "The smallest number you entered was: " + smallestnumber);
}
}
Re: Confused about Arrays and JOptionPane!
I guess I'm overlooking something here...
I've simplified the program for the sake of learning the basics. Have a look.
Code java:
import javax.swing.JOptionPane;
public class SmallestElement {
static int index;
static double[] list = new double[10];
static double smallestnumber;
public static void main(String[] args) {
//Store user input
JOptionPane.showMessageDialog(null, "Enter ten integers: ");
list[0] = Integer.parseInt(JOptionPane.showInputDialog(null, "1st number" + " "));
list[1] = Integer.parseInt(JOptionPane.showInputDialog(null, "2nd number" + " "));
list[2] = Integer.parseInt(JOptionPane.showInputDialog(null, "3rd number" + " "));
list[3] = Integer.parseInt(JOptionPane.showInputDialog(null, "4th number" + " "));
list[4] = Integer.parseInt(JOptionPane.showInputDialog(null, "5th number" + " "));
list[5] = Integer.parseInt(JOptionPane.showInputDialog(null, "6th number" + " "));
list[6] = Integer.parseInt(JOptionPane.showInputDialog(null, "7th number" + " "));
list[7] = Integer.parseInt(JOptionPane.showInputDialog(null, "8th number" + " "));
list[8] = Integer.parseInt(JOptionPane.showInputDialog(null, "9th number" + " "));
list[9] = Integer.parseInt(JOptionPane.showInputDialog(null, "10th number" + " "));
// call min method
min();
}
public static void min(){
//Find the Smallest Number in the array
int minIndex = 0;
for (index = 0; index < list.length; index++)
if (list[minIndex] > list[index])
minIndex = index;
smallestnumber = list[minIndex];
JOptionPane.showMessageDialog(null, "The smallest number you entered was: " + smallestnumber);
}
}
Now if I use the prompted header, I get compile errors that say...
"invalid method declaration; return type required"
Here is the messed up code.
Code java:
import javax.swing.JOptionPane;
public class SmallestElement {
static int index;
static double[] list = new double[10];
static double smallestnumber;
public static void main(String[] args) {
//Store user input
JOptionPane.showMessageDialog(null, "Enter ten integers: ");
list[0] = Integer.parseInt(JOptionPane.showInputDialog(null, "1st number" + " "));
list[1] = Integer.parseInt(JOptionPane.showInputDialog(null, "2nd number" + " "));
list[2] = Integer.parseInt(JOptionPane.showInputDialog(null, "3rd number" + " "));
list[3] = Integer.parseInt(JOptionPane.showInputDialog(null, "4th number" + " "));
list[4] = Integer.parseInt(JOptionPane.showInputDialog(null, "5th number" + " "));
list[5] = Integer.parseInt(JOptionPane.showInputDialog(null, "6th number" + " "));
list[6] = Integer.parseInt(JOptionPane.showInputDialog(null, "7th number" + " "));
list[7] = Integer.parseInt(JOptionPane.showInputDialog(null, "8th number" + " "));
list[8] = Integer.parseInt(JOptionPane.showInputDialog(null, "9th number" + " "));
list[9] = Integer.parseInt(JOptionPane.showInputDialog(null, "10th number" + " "));
// call min method
min(array);
}
public static min(double[] array){
//Find the Smallest Number in the array
int minIndex = 0;
for (index = 0; index < list.length; index++)
if (list[minIndex] > list[index])
minIndex = index;
smallestnumber = list[minIndex];
JOptionPane.showMessageDialog(null, "The smallest number you entered was: " + smallestnumber);
}
}
I have just finished the chapter on method calls, and am still a bit rusty on how they work, but I promise I am trying as hard as I can. I want to really understand this stuff.
Re: Confused about Arrays and JOptionPane!
"return type required"
The error message says it all. You min method doesn't have a return type. Compare it to the main method or any other method for that matter.
Re: Confused about Arrays and JOptionPane!
You don't have a variable called array in main method. But you do have a double array that could work called list.
Try changing it to list.
i.e.
As for your sum thing, I suggest you have like
Code java:
int sum = 0;
for (int i = 0; i < list.length; i++)
{
sum = sum + list[i];
}
instead of manually adding them all.
You min method should be return type void as it isn't returning anything.
Also, if you're allowed to deviate slightly from what you had earlier when you asked for the 10 integers, put:
Code java:
for (int i =0; i < list.length; i++)
{
list[i] = Integer.parseInt(JOptionPane.showInputDialog(null, "number " + i ++ " "));
}