I'm trying to write an application that inputs three integers from the user and displays the sum, average, product, smallest and largest of the numbers. It also should print the three numbers in an ascending order (from smallest to the largest). I tried to return the results into one JOptionPane but the sortResult is not returning the user inputs. I probably tried to over simplify the sort algorithm and am not applying it correctly. Currently the sortResult is returning a jumbled string:
"Your numbers are: [l@23ab93od"
What is the easiest way to correct this? Here is the code:
// Calculations using three integers.
import java.util.Arrays;
import javax.swing.JOptionPane;
public class Calculations {
public static void main( String args[] ){
int x;// first number
int y;// second number
int z;// third number
int sumResult;// sum of numbers
int avgResult;// average of numbers
int prodResult;// product of numbers
int maxResult;// max of numbers
int minResult;// min of numbers
String xVal;// first string input by user
String yVal;// second string input by user
String zVal;// third string input by user
xVal = JOptionPane.showInputDialog("Enter first integer:");// prompt
yVal = JOptionPane.showInputDialog("Enter second integer:");// prompt
zVal = JOptionPane.showInputDialog("Enter third integer:");// prompt
x = Integer.parseInt( xVal );
y = Integer.parseInt( yVal );
z = Integer.parseInt( zVal );
sumResult = x + y + z;
avgResult = sumResult/3;
prodResult = x * y * z;
maxResult = Math.max(x, Math.max(y, z));
minResult = Math.min(x, Math.min(y, z));
int[] sortResult = {x, y, z};
Arrays.sort(sortResult);
String result;
result = "Your numbers are: " + sortResult +
"\n" + "The sum is " + sumResult +
"\n" + "The average is " + avgResult +
"\n" + "The product is " + prodResult +
"\n" + "The max is " + maxResult +
"\n" + "The min is " + minResult;
JOptionPane.showMessageDialog(null, result, "Results", JOptionPane.INFORMATION_MESSAGE );
System.exit(0);
}// end method main
}// end class Product
When you concatenate an object with a String, Java automatically converts to a string by using its toString
method. The array type does not override the toString
method, so when toString
is used on it, it will use Object
's toString
method, which just returns the type of the object ( [l
is the type of an array of ints) @
its hash code in hex.
To get a String that represents what an array contains, use Arrays.toString(arrayName)
, (in this case "Your numbers are: " + Arrays.toString(sortResult) + ...
).
Also, since you know that your array contains three elements, you could also just use something like sortResults[0] + ", " + sortResults[1] + ", " + sortResults[2]
, if you wanted them to be in a different format that the one returned by Arrays.toString
(if you did not know the number of elements, or had too many to type them all one by one, you would have to loop through them).
You can use Arrays.toString(array)
or the other nice thing you can do is to use collection framework.
Here I am giving you an example
ArrayList<Integer> al = new ArrayList<>();
// to add three element or to do any common thing for any number of elements.
for(int i = 0; i < 3; i++)
al.add(JOptionPane.showInputDialog("Enter " + i + "th number:"));
System.out.println(al);
By using Collections provided by java, you can make sorting, finding max, min, etc very easy.
// finding minimum of all elements
Integer min = al.stream().min((Integer a, Integer b) -> a.compareTo(b));
// finding maximum of all elements
Integer max = al.stream().max((Integer a, Integer b) -> a.compareTo(b));
// sorting all elements
al.sort((Integer a, Integer b) -> a.compareTo(b));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.