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

Thread: JOptionPane Question/ Printing

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JOptionPane Question/ Printing

    I am not sure what I am doing that is causing it not to print. I havent completly finished the program yet, I have decided to just try and get it to print right first...haha. My math was right but it wouldnt loop once I set it up to loop it won't print...any guidance would be appreciated.




    This is what I am working on.....right now I am just trying to get it to print message1 when the input is zero. Any help would be appreciated. Here is what I have so far.

     
    import java.util.Scanner;//program that uses a class scanner
    import java.lang.Float;
    import javax.swing.JOptionPane; //Dialog Boxes
     
    //Program that reads numbers 
     
    public class NumberCounter
    {
    	public static void main(String args[])
    	{
    		Scanner input = new Scanner( System.in );
     
    		int Entries = 1;    //Total of all numbers entered
    		int Total = 0;   //Total sum of numbers
    		int Large;  //Largest number in group
    		int Small;  //Smallest number in group
    		float Average;        //Average of numbers entered	
     
    	do{
    		String counter=	
    		JOptionPane.showInputDialog("Input numbers to check:");	
    		float Number1 = Float.parseFloat(counter);	
     
    		Entries ++;
    		Total += Number1;
                	                     Average = Total / Entries;
     
    	}while(Number1 != 0);
     
                         String message1=
    		String.format("Total number of Entries : %d\n Numbers entered: %.0f\n Average of all numbers:%.0d\n 
                                              Total sum of all numbers:%d", Entries, Number1, Average, Total);
    		JOptionPane.showMessageDialog(null, message1);
     
    	} //end method main
     
    } //end class NumberCounter
    Last edited by 03EVOAWD; September 3rd, 2009 at 01:56 PM.


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Smile Re: JOptionPane Question/ Printing

    i edited your code sir...
    import javax.swing.*;
    public class LoopSample8
     
    {
     
         public static void main(String[] arggs)
     
        {
            int keyIn1,
                keyIn2,
                largest,
                smallest,
                sum;
            float  average;
     
            int numberOfEntries = 0;
            sum = 0;
     
     
            int loopKey = 1;
     
            //you have to accept a single data first outside the loop
            //the data in this input will serve as a comparison for the largest and smallest
     
            keyIn1 = Integer.parseInt(JOptionPane.showInputDialog("Enter A Number"));
     
            largest = keyIn1;    //to make comparison for the next numbers you have to assign
            smallest = keyIn1;   //the first data into these variables
            sum = keyIn1;        //this is fine if you not assign it in this variable sum
                                 //   "BUT" it depends on the problem of the program
     
     
            do
            {
                //next
                //get the next input values.. until it meets the condition
                keyIn2 = Integer.parseInt(JOptionPane.showInputDialog("Enter A Number"));
     
                sum = sum + keyIn2;
                loopKey++;
     
                //to get how many entries were entered you have to compare it to the data that was keyed-in in keyIn2
                //no matter what is the value in keyIn2
                //it will add an Entry for the variable numberOfEntries
                if(numberOfEntries <= keyIn2 || numberOfEntries >= keyIn2 || numberOfEntries != keyIn2)
                    numberOfEntries = numberOfEntries + 1;
     
                /*next
                 *compare the values of keyIn2 to the largest that has already the value of keyIn1
                 *you have to make a twice condition
                 *make an expression that will not compare the value of keyIn2 in "0" (zero)
                 *1.)your zero is the terminating value(the condition to execute)
                 *2.)if you not do this expression the SMALLEST NUMBER will always be "0"(zero)
                 */
                if(keyIn2 <= smallest && keyIn2 !=0)
                    smallest = keyIn2;
     
                //same here
                else if(keyIn2 >= largest && keyIn2 !=0)
                    largest = keyIn2;
            }
            while(keyIn2 != 0); //the program will continue to loop until you "enter" "0" zero
     
     
            /*we have to make another condition for the keyIn1 here,(outside the loop)
             *because the loop doesnt do anything about the objects outside of it
             *if you don not make a condition for an entry here
             *you will miss a single entry
             *because the loop will only provide you the values that are inside of it
             */
     
            if(numberOfEntries <= keyIn1 || numberOfEntries >= keyIn1 || numberOfEntries != keyIn1)
                    numberOfEntries = numberOfEntries + 1;
     
            //you have to determine the average here not inside for some valid logical state
           // if it is inside the loop it will get average continuesly
            average = sum / (numberOfEntries - 1);
     
     
            JOptionPane.showMessageDialog(null,"Entries:  " + numberOfEntries + "\n" +
                                               "Sum:  " + sum + "\n" +
                                               //"Average:  " + average + "\n" +
                                               "Smallest:  " + smallest + "\n" +
                                               "Largest:  " + largest);
        }
     
    }

    you have Scanner class .. why is it there? its only use for console input..

    Last edited by chronoz13; August 28th, 2009 at 02:30 AM.

  3. The Following User Says Thank You to chronoz13 For This Useful Post:

    03EVOAWD (August 31st, 2009)

  4. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane Question/ Printing

    Thanks for the help I was able to figure out what I was doing wrong.....
    I really appreciate how you broke it down in the text for me that went a long way for me....thanks again

    I have scanner class just because I am new to java and am still learning.
    Last edited by 03EVOAWD; September 3rd, 2009 at 07:14 AM.

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. Printing a JTable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 08:15 AM
  3. How to printing a Jtable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 06:57 AM
  4. Printing JTable that retrieve data from the Database
    By hundu in forum AWT / Java Swing
    Replies: 3
    Last Post: June 28th, 2009, 01:50 PM
  5. Printing a Histogram Help - Arrays
    By Mock26 in forum Collections and Generics
    Replies: 1
    Last Post: June 4th, 2009, 04:49 AM