Ok, I have a homework problem that I'm needing some help with.
The program has to
Allow the user to specify the number of integers to be inputted into the array
read a data set of number of integers into the array
Determine the sum
find the largest integer
and deturmine how many times the largest number is in the array.
I'm having the most trouble with find the largest number, I know how to do it with a really long if, else statement, but there has to be an easier way.

import java.util.Scanner;
public class lab10a_Killackey {
    public static void main(String args[])
    {
        Scanner input = new Scanner (System.in);
        int dataset;
        int [] integers = new int[dataset] ;
        int total = 0;

        System.out.println("How many integers are in the data set? ");
         dataset = input.nextInt();
        System.out.println("Enter the integers in the dataset 1 per line");
        int [dataset] integers = {input.nextInt()};


        if ( dataset != 0)
        {
            for (int number: integers );
                total += number;

            System.out.printf("Sum of integers is: %d\n",total);

            for (int number: integers);
            //This is where the code for finding the largest integer is supposed to be!

            for (int largest = 1; largest<= dataset; dataset++ )
                System.out.printf("This number appered %d times\n");

        }
        else
            System.out.print("The dataset is empty");
    }
}

Recommended Answers

All 6 Replies

"I'm having the most trouble with find the largest number"

do you ever get more than 1 number in your array? i am not sure how you expect to populate your array without a loop.

The loop to populate should be a for loop, to add to what the person above me said.

edit: I will check back on this tomorrow if I remember, I'm too tired right now. Sorry!

So i need to have a loop first to have more than one number inputed into the array? So it should look like this:

    Scanner input = new Scanner (System.in);
    int dataset;
    int [] integers = new int[dataset] ;
    int total = 0;

    System.out.println("How many integers are in the data set? ");
     dataset = input.nextInt();
    System.out.println("Enter the integers in the dataset 1 per line");
    int [dataset] integers = {input.nextInt()};
    for (int cnt = 1; cnt <= dataset; cnt++ )
        int [dataset] integers = {input.nextInt()};

Then continue on with the program.

not necessarily, your syntax looks like you are trying to create a complete array using {}. but, you are using nextInt, which is only 1 token, so you are only going to get 1 int value. using that syntax is possible, but you need to create the whole array at once. alternatively you should use a for loop as BestJewSinceJC suggested and populate the array 1-by-1.

good luck

OOOoooohhh Ok that makes sense. Thanks for yall's help.

no worries, let us know if you run into any issues.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.