Hi, I have set-up a while loop to iterate an amount specified by the user. However, is it possible to compare two numbers entered by a user inside the loop and then have the larger number replace the smaller? I am having trouble because I have the first result being stored as "num" but when the loop re-iterates how can I compare these inputs if they have the same name? Like, " if (num > num) "...that won't work.

you can do like this, at first, before entering loop, get a number from user and store it in, say "num1" then in the loop get another number "num2" then compare them.

you can do like this, at first, before entering loop, get a number from user and store it in, say "num1" then in the loop get another number "num2" then compare them.

Well, I think I need to ask for the input after the program enters the loop. Since I am having the program 'repeat' itself as much as the user decides, all comparisons of the numbers have to take place in the while loop.

Is there a way to store a number and if it is larger/smaller, replace it with a new number and so on, in a while loop?

Of course there is, and there is many ways of doing this.

``````x = 5;

/loop
//parse a string entered by a user
if (inputedNumber < x || inputedNumber > x)
{
//inputedNumber = aNewValue
}

//end loop``````

well another solution can be like this but you need at least 2 variable in order to compare something (1. variable is for saving entered number, 2. variable holds the current entered value) but this time you need to give 1. variable some value.
This values that you will give to 1.variable is important, say your 1. variable stores the biggest number that the user entered so far. so you need to set 1. variable 0,
but if you are going to store the smallest number, then you will need to set 1.variable to maximum.
this way actually no different from my previous solution, but this time programmer gives the first number.

well another solution can be like this but you need at least 2 variable in order to compare something (1. variable is for saving entered number, 2. variable holds the current entered value) but this time you need to give 1. variable some value.
This values that you will give to 1.variable is important, say your 1. variable stores the biggest number that the user entered so far. so you need to set 1. variable 0,
but if you are going to store the smallest number, then you will need to set 1.variable to maximum.
this way actually no different from my previous solution, but this time programmer gives the first number.

Ok, I am trying my best to understand..I am having a little trouble understanding the comparision process.

In the while loop how do I replace and store the correct integer. For example, if the user enters a 4 it will be stored, then if the user enters a 7 how can that replace the 4? then if a user enters a 10 it should replace the 7. Sorry, I going to keep reading your post and hopefully I will figure it out.
I understand that you need two variables to compare but how? Here is how my while loop starts.

``````//loop
while (intCounter <= numOfints)
{

// first user input
numstring = JOptionPane.showInputDialog( "Enter an integer" );

num = Integer.parseInt( numstring );``````

I guess my main problem is how to compare a number in the loop with the same variable.

well here how the second variable is used :

``````int num, oldnum; //there are our 2 variable
/*before entering the loop either we ask a number from the user or we assign it by ourselves,
oldnum = 0;
*/

numstring = JOptionPane.showInputDialog( "Enter an integer" );

oldnum = Integer.parseInt( numstring );

while (intCounter <= numOfints)
{

// first user input
numstring = JOptionPane.showInputDialog( "Enter an integer" );

num = Integer.parseInt( numstring );

//now we have two interger in our hands
//we can make comparisons...
//say we need to keep the bigger one...
if (num > oldnum) oldnum = num;
}``````

thanks a bunch, i now know where I was going wrong..

thanks again

well here how the second variable is used :

``````int num, oldnum; //there are our 2 variable
/*before entering the loop either we ask a number from the user or we assign it by ourselves,
oldnum = 0;
*/

numstring = JOptionPane.showInputDialog( "Enter an integer" );

oldnum = Integer.parseInt( numstring );

while (intCounter <= numOfints)
{

// first user input
numstring = JOptionPane.showInputDialog( "Enter an integer" );

num = Integer.parseInt( numstring );

//now we have two interger in our hands
//we can make comparisons...
//say we need to keep the bigger one...
if (num > oldnum) oldnum = num;
}``````