I'm extremely new to Java as well as programming so please bare with me. I'm writing a test program that asks the user to input 10 numbers then outputs the smallest number. I'm trying to convert this to an input dialog box...
public class SmallestElement { public static void main(String[] args) { double[] numbers = new double[10]; java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter ten numbers: "); for (int i = 0; i < numbers.length; i++) numbers[i] = input.nextDouble(); System.out.println("The minimum value is " + min(numbers)); } public static double min(double[] array) { double min = array[0]; for (int i = 1; i < array.length; i++) if (min > array[i]) { min = array[i]; } return min; } }
I tried to convert it over to JOptionPane but when the box pops up to enter 10 numbers, it's not exactly accepting the input. Here's the code for trying to change it over.
import javax.swing.JOptionPane; public class JSmallestElement { public static void main(String[] args) { double[] numbers = new double[10]; String input = JOptionPane.showInputDialog("Enter ten numbers: "); for (int i = 0; i < numbers.length; i++) numbers[i] = Double.parseDouble(input); JOptionPane.showMessageDialog(null, "The minimum value is " + min(numbers)); } public static double min(double[] array) { double min = array[0]; for (int i = 1; i < array.length; i++) if (min > array[i]) { min = array[i]; } return min; } }
I obviously have no clue what I'm doing so any help would be greatly appreciated! I cannot get past how confusing this stuff is. Thank you!