Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Convert to JOptionPane

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Convert to JOptionPane

    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!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Convert to JOptionPane

    it's not exactly accepting the input.
    What do you mean by this? Does it throw an exception? Not behave properly? If a user enters a String - something like "10 5 1 100.....43", you need to get each number individually. This is where spending some time reviewing the API is helpful, as there are several methods to do this (see String (Java Platform SE 6) ) for example indexOf, split, using a StringTokenizer, etc...once you have these individual values you can parse them using Double.parseDouble

Similar Threads

  1. Using Icons in JOptionPane
    By Jchang504 in forum AWT / Java Swing
    Replies: 3
    Last Post: September 19th, 2014, 02:28 PM
  2. While JOptionPane equal Yes????
    By maximus20895 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2010, 08:57 PM
  3. Display in JOptionPane
    By t-rank in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 19th, 2010, 12:09 AM
  4. Change to JOptionPane
    By Liuric in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 12:07 AM
  5. JOptionPane using If and Else
    By Liuric in forum Member Introductions
    Replies: 7
    Last Post: October 1st, 2010, 12:05 AM