# Solved: Java to find Largest Number



## Carlos_Mendoza (Nov 12, 2011)

I have a String Array, that captures 10 numbers from a user. I now need to know how to find the largest number that was entered by the user. This is the code that I am using to this point:

```
public class Test
{
    public static void main (String args[])
    {
    
    int tick;
    
    String[] arrayuserinput = new String[11];
    for (tick = 1; tick < arrayuserinput.length; tick++)
        {
        
        arrayuserinput[tick] = JOptionPane.showInputDialog("Enter Number" + "# " + tick + ":");
        
        if (Integer.valueOf(arrayuserinput[tick]) > 0 && Integer.valueOf(arrayuserinput[tick]) <= 29)
        {

        }    
            else
            {
                JOptionPane.showInputDialog("Please enter a number between 0 and 29");
            }

        }
        
                
        //Terminate the Application
        System.exit(0);
    }
}
```
But I don't know how to go back in and add a way to find the largest number entered?


----------



## andythepandy (Jul 2, 2006)

Hi again! I like it when there are lots of Java questions 

I have modified your code in a few places to make it easier to find the biggest:

```
public static void main(String args[]) {

	int tick;
	
	[COLOR="Red"]int biggest = 0;[/COLOR]

	[COLOR="red"]int[][/COLOR] arrayuserinput = new[COLOR="red"] int[11][/COLOR];
	for (tick = 1; tick < arrayuserinput.length; tick++) {
		
		//variable to hold what the user has entered
		[COLOR="red"]int userInput = 0;[/COLOR]
		
		//this loop will keep asking the user for the number if they enter an incorrect numer
		[COLOR="red"]while (userInput <= 0 || userInput > 29) {
			//get the input
			userInput = Integer.valueOf(JOptionPane.showInputDialog("Enter Number between 0 and 29\n" + "# " + tick + :"));
		}[/COLOR]
		//put the input out array
		arrayuserinput[tick] = userInput;
		
		//use Math.max to find the largest number.
		//this is equivalent to:
		
		//if (userInput > biggest)
		//	biggest = userInput;
		
		[COLOR="red"]biggest = Math.max(userInput, biggest);[/COLOR]
	}
	//tell everyone what the biggest was
	JOptionPane.showMessageDialog(null, "Biggest was " + biggest);

	// Terminate the Application
	System.exit(0);
}
```
Major differences:

+I've changed the arrayuserinput to an int array to hold the numbers the user enters, not the string they have entered
+I've added a while loop. This is the validation stage, if the number is out of range then the condition in the while brackets will be true so the code inside the while block will be executed. Notice with your previous method you could enter an incorrect number if you entered an out of range number twice in a row.
+Added the Math.max function. This simply finds whether out new number is bigger than our current 'biggest' number. If it is, we change 'biggest' to reflect this

Hope that description + the comments will explain it to you

p.s. You can improve validation by checking to see if the user has entered a number, and not something like a letter or a symbol


----------



## Carlos_Mendoza (Nov 12, 2011)

Glad you enjoy the java ?'s because I am a total NOOB and only know what I am able to google (or get help from you with  )

I am reading through the comments and they seem to make sense...I am going to practice run, and (I am sure it will be fine) hope I can make sense of what is going on.

EDIT --- 
One thing I noticed is that if the user enters an invalid number, there is no prompt that shows the number was invalid it only asks for the number again?


----------



## Carlos_Mendoza (Nov 12, 2011)

Other than that the code looks/works/functions perfectly.


----------



## johnripper1987 (May 3, 2012)

I'm an newbie in JAVA? Can you post some good example like this for me? Sorting example maybe?


----------



## andythepandy (Jul 2, 2006)

Take a look at the great tutorials here - http://thenewboston.org/list.php?cat=31

There's good basic examples here - http://www.java-examples.com/


----------

