Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

Solved: Java to find Largest Number


(!)

Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 113 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
07-May-2012, 09:04 AM #1
Solved: Java to find Largest Number
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:
Code:
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's Avatar
andythepandy andythepandy is offline
Computer Specs
Member with 365 posts.
 
Join Date: Jul 2006
Location: England
Experience: Intermediate to Advanced
07-May-2012, 10:07 AM #2
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:
Code:
public static void main(String args[]) {

	int tick;
	
	int biggest = 0;

	int[] arrayuserinput = new int[11];
	for (tick = 1; tick < arrayuserinput.length; tick++) {
		
		//variable to hold what the user has entered
		int userInput = 0;
		
		//this loop will keep asking the user for the number if they enter an incorrect numer
		while (userInput <= 0 || userInput > 29) {
			//get the input
			userInput = Integer.valueOf(JOptionPane.showInputDialog("Enter Number between 0 and 29\n" + "# " + tick + :"));
		}
		//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;
		
		biggest = Math.max(userInput, biggest);
	}
	//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's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 113 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
07-May-2012, 10:23 AM #3
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?

Last edited by Carlos_Mendoza; 07-May-2012 at 10:29 AM..
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 113 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
07-May-2012, 11:03 AM #4
Other than that the code looks/works/functions perfectly.
johnripper1987's Avatar
johnripper1987 johnripper1987 is offline
Member with 10 posts.
 
Join Date: May 2012
Experience: Beginner
09-May-2012, 05:16 AM #5
I'm an newbie in JAVA? Can you post some good example like this for me? Sorting example maybe?
andythepandy's Avatar
andythepandy andythepandy is offline
Computer Specs
Member with 365 posts.
 
Join Date: Jul 2006
Location: England
Experience: Intermediate to Advanced
09-May-2012, 06:15 PM #6
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/
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑