1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Java to find Largest Number

Discussion in 'Software Development' started by Carlos_Mendoza, May 7, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    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?
     
  2. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    367
    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;
    	
    	[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
     
  3. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    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?
     
  4. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    Other than that the code looks/works/functions perfectly.
     
  5. johnripper1987

    johnripper1987

    Joined:
    May 3, 2012
    Messages:
    10
    I'm an newbie in JAVA? Can you post some good example like this for me? Sorting example maybe?
     
  6. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    367
  7. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1052272