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 4 of 4

Thread: Finding the Biggest Element in an Array

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Finding the Biggest Element in an Array

    Hi I'm really struggling with this homework assignment, I'd appreciate it if you helped me out!

    this is the instruction:
    Create a program called BiggestElement that will allow the user to input five integers. The program will then output the largest of the five numbers.*Include a function-type method that will return to the main method the value of the largest element in the array.*Use the length method to help determine the number of elements in the array.

    My problem is that when I run the program it states the first input as the biggest element instead of the biggest number. I think its something wrong with my bubble sorting but I'm not sure.
    this is my code:

    **import java.io.*;
    public class BiggestElement
    {
    public static void main (String[] args) throws IOException
    {
    double favNums[] = new double [6];

    populate (favNums);

    System.out.println ("The largest value is " + findBiggest (favNums));
    } // main method


    public static void populate (double favNums[]) throws IOException
    {
    DataInputStream input = new DataInputStream (System.in);

    for (int x = 0 ; x < 5 ; x++)
    {
    System.out.print ("Enter any number: ");
    favNums [x] = Double.parseDouble (input.readLine ());
    }
    } //populate class


    public static double findBiggest (double favNums[])
    {
    double biggest = favNums [0];
    double hold;

    for (int x = 1 ; x < 5 ; x++)
    {
    if (favNums [x] > favNums[x + 1])
    {
    hold = favNums [x];
    favNums [x] = favNums [x + 1];
    favNums [x + 1] = hold;
    }
    }

    return biggest;

    } //findBiggest class
    } // BiggestElement class**
    Last edited by herme; January 19th, 2021 at 08:03 PM.

  2. #2
    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: Finding the Biggest Element in an Array

    How are you trying to debug the code to find the problem?
    I use print statements to print out the values of variables as their values are changed and as they are used.
    The print out will show you see what the computer sees and help you find the problem.
    For example add this at the end of the findBiggest method to see the sorted array:
     System.out.println("favNums= " + java.util.Arrays.toString(favNums));

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Note:The Scanner class has easier to use methods for reading numeric input from a user.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    herme (January 19th, 2021)

  4. #3
    Junior Member
    Join Date
    Jan 2021
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Finding the Biggest Element in an Array

    I have found that the problem is that I need to change favNums[0] to the biggest number but every correction i make is an error.

  5. #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: Finding the Biggest Element in an Array

    Why is there a sort for a search problem?
    Can you list the steps in English that the program needs to take to search for the biggest number?
    You need to have a design(list of steps) for the actions of the program before trying to write any code.

    every correction i make is an error.
    Please post the new code. Be sure to wrap it in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: February 13th, 2014, 04:39 AM
  2. How to find where the biggest number is in an array
    By Jazz26 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 2nd, 2013, 06:07 AM
  3. Find Biggest Number in Array
    By jo15765 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 12th, 2012, 03:01 PM
  4. [SOLVED] Finding biggest files, making list
    By CjStaal in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 20th, 2012, 11:15 PM
  5. Finding the smallest element and shifting....
    By DeadlySwordz in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2011, 03:57 PM