Jump to content

Outputing Smallest and Largest Integer

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Kinsleyy

Kinsleyy

    Newbie

  • Members
  • Pip
  • 9 posts
I am trying to write an application that reads 5 integers and then reads and prints the largest and smallest integers in the group. This is what I have.




import javax.swing.JOptionPane;


public class largestsmallest {

         public static void main(String[] args) {

      

    int counter=0;

    int number=0;

    int largest = number;

    int smallest = number;

       

                while (counter<5){

                	counter++;

                	number = Integer.parseInt(JOptionPane.showInputDialog("Enter number"));


                         

                	if(number > largest){largest=number;

                	JOptionPane.showMessageDialog(null," largest " + largest, "largest", JOptionPane.PLAIN_MESSAGE);

                	}

             

              


                      

                	else if(number < smallest)smallest=number;{

            	        JOptionPane.showMessageDialog(null," smallest" + smallest, "smallest", JOptionPane.PLAIN_MESSAGE);


                	}

    } 

   

        

       

       

        System.exit(0);

    } //end main

} //end class




#2
Kinsleyy

Kinsleyy

    Newbie

  • Members
  • Pip
  • 9 posts
I cannot find where my error is. Any help will help, thanks guys

#3
CallinWire

CallinWire

    Newbie

  • Members
  • Pip
  • 8 posts
Two things. First, is that the smallest and largest integers, initially, are zero, instead of the largest and smallest possible values. You should have Integer.MAX_VALUE and Integer.MIN_VALUE for the initial values of the smallest and largest.

Also, you've got a bracket out of place here:
else if(number < smallest)smallest=number;[COLOR=RED]{[/COLOR]


#4
Kinsleyy

Kinsleyy

    Newbie

  • Members
  • Pip
  • 9 posts
I cannot find where the bracket needs to be.

#5
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
right after the if-clause.
Pretty logical, if you ask me...

#6
Kinsleyy

Kinsleyy

    Newbie

  • Members
  • Pip
  • 9 posts
Thanks! I have the app working. The bracket was logical too..srry