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 5 of 5

Thread: Confused about Arrays and JOptionPane!

  1. #1
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.
       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

    public static min(double[] array)
    for example...

    Any advice would be great.

    Neil
    Last edited by Java Neil; March 2nd, 2011 at 12:01 AM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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.

    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);
     
    	}
     
       }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

       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.

       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.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool 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.

     min(list);


    As for your sum thing, I suggest you have like
    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:

    for (int i =0; i < list.length; i++)
    {
     list[i] = Integer.parseInt(JOptionPane.showInputDialog(null, "number "  + i ++ " "));
     }
    Last edited by javapenguin; March 4th, 2011 at 05:05 PM. Reason: Added another suggestion

Similar Threads

  1. Confused with Arrays
    By Solidius in forum Collections and Generics
    Replies: 3
    Last Post: October 29th, 2010, 09:54 AM
  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