Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Find Biggest Number in Array

  1. #1
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Find Biggest Number in Array

    I am trying to find the Biggest number in an array. I can't seem to get my coding down pat with it.
    		myarray[size] = JOptionPane.showInputDialog("Enter Number" + size + ":");
     
    		if (Integer.valueOf(myarray[size]) > 0 && Integer.valueOf(myarray[size]) <= 30)
    		{
     
    		}	
    		    //What to do if number entered does not fit criteria
    			else 
    			{
    				JOptionPane.showInputDialog("Please enter a number between 0 and 30");
    			}
    			if (myarray[size] > Biggest)
    			{
    				Biggest = myarray[size];
    				return int Biggest;
    			}
     
    		}

    This is what I am working with and like I said for some reason I can't get my code to return the biggest number that was entered.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Find Biggest Number in Array

    Does your code compile? If not and you can't understand the compiler messages, post them. Also post the code they refer to rather than just a small snippet.

    I am trying to find the Biggest number in an array.
    Before writing even a line of code decide *how* you are going to go about finding the biggest number in the array. This begins, not as a computer or coding problem, but as one of analysis and expression. Confronted with a long list of numbers how would you, yourself, determine the largest? What (if anything) would you have to remember as you worked through the problem? How many things would you have to remember?

    Such a "plan of attack" will form the basis of your code.

  3. #3
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Find Biggest Number in Array

    No it does not compile. When I try to compile that code, I get an error of:
    error: cannot return a vlue from a method whose result type is void


    To answer your questions:
    --My plan of attack on how to write the code was all laid out, this was an "after-thought" that was requested to be added in. I feel like maybe I should have a variable that stores each number as it is entered into the array, and compare it to the previous number to see if it is larger. So for example maybe
    int x
    int y
     
    if x > y then keep X and let y be replaced by the next number in the array

    I just am not sure how to use java to code something like that.

    I have also tried using this:
    				int[] numbers = {myarray};
    				int max = 0;
    				for(int i=0;i<number.length;i++)
    				{
    					if(numbers[i] > max)
    					{
    						max = numbers[i];
    					}
    				}
    					JOptionPane.showInputDialog("The Largest Number entered is: ");

    But the error I get from this (I think) is complaining because I want to pull an int type from a string array!
    Last edited by jo15765; May 7th, 2012 at 07:52 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Find Biggest Number in Array

    error: cannot return a value from a method whose result type is void
    Do you understand what the compiler is complaining about? If you want to return a value from a method, you must tell the compiler about it before you do it. Defining a method with void is telling the compiler that the method will not return any value.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    May 2012
    Posts
    36
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Find Biggest Number in Array

    Ah, that makes sense. I was barking up the wrong tree thinking the error was due to trying to pull an int from a string array. Let me work on the coding a little bit and declare my statements a little different and see what I can come up with.

  6. #6
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Find Biggest Number in Array

    try using an enhanced for loop instead of a normal for loop

    for example:

    package stuff;
     
    public class tester
    {
    	public static void main(String [] args)
    	{
    		int[] array = {5,2,7,5,6};
    		int largest = 0;
     
    //enhanced for loop here
     
    		for(int i : array)
    		{
    			if(i > largest)
    			{
    				largest = i;
    			}
    		}
    		System.out.println(largest);
     
    	}
    }

    This would print out the number 7. the enhanced for loop is for running through an array, list, arraylist, etc... It lasts as long as the length of what you're sorting through. It takes the first value from array, in this case 5, and puts it into " i " and from there, it runs the code in the loop until it runs out of values in the array.

Similar Threads

  1. Replies: 5
    Last Post: April 22nd, 2013, 07:27 AM
  2. Find the two largest number
    By vendettabf in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 29th, 2011, 01:23 PM
  3. How to find the larget number on binary search tree
    By Jurgen in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 24th, 2011, 11:26 AM
  4. find the sum of even number not exceed four million
    By i4ba1 in forum Algorithms & Recursion
    Replies: 10
    Last Post: June 29th, 2011, 08:08 AM
  5. Find total number less than average
    By maximus20895 in forum Collections and Generics
    Replies: 2
    Last Post: December 1st, 2010, 01:46 PM