Hi. im doing some exercises and im confused as to what its asking me to do.

Problem: write a program that displays the average and highest mark based on the sets of marks entered. values less than 0 and greater than 100 should be ignored

input: a list of input that contains integer with the range and inclusive of 0 to 100 and ends with -1

i did some coding(to test) but still confused:

import java.util.Arrays;
import java.util.Scanner;

public class meanAndMax {

    public static void main(String[] args) {
        //create/initialize scanner
        Scanner sc = new Scanner(System.in);

        //float result;

        System.out.println("How many numbers would you like to insert?");
        int num = sc.nextInt();
        int[] array = new int[num];

        System.out.println("Please enter the data : ");

        for(int i=0; i<array.length; i++){
            array[i] = sc.nextInt();
        }

        System.out.println(Arrays.toString(array));

        sc.close();
    }

}

i used array to store the marks to be entered but is that the right way? can i just insert the marks without knowing the array size but itll just expand on its own? because size of array is not given so i have to prompt the user for the how many numbers they want to insert.

But to me like if i was to key in marks i dont want to have to count how many marks there are only then i can key in the marks i would rather just key in the marks straight away. is there a different way to approach this? sorry new to java doing a few different exercises.

i was thinking like the users enter numbers then a count variable will count how many has been inserted then go from there but then i would need to store the numbers somewhere so this way i wouldnt have to prompt user for how many marks they want to enter.

guidance much appreciated.

thanks in advance!

That's how arrays work! You have to know the size to create the array.
Java comes with things called Lists, that you can create with no entries, then add as many entries as you want. They automatically expand and contract to fit the data. That's what you would normally use in a situation like this. You'll learn about those later, but for now you should work with arrays because they are a fundamental programming structure that everyone needs to understand before moving on.

oh alright. i thought there would be another way and i guess thats Lists but we havent gone into that yet. thanks for clarifying that up! :) i also have a question about "ends with -1", according to the question do i add it to the end of the array? like this :

for(int i=0; i<array.length; i++){
            array[i] = sc.nextInt();
        }

        array[array.length + 1] = -1;

but then say i entered 1 2 3 4 5 6 7 8 9 10 then the output would be 1 2 3 4 5 6 7 8 9 -1

or is it the user that enter the -1? im confused.

You don't really need arrays as far as I see.

What you need are a few variables: a counter, to count how many (valid) inputs were read, the sum of those, and later you calculate the median by those.

stultuske, so the counter increments by 1 everytime a number is entered ?

no. every time a valid number is entered. so, values in the interval [0, 100] -> when these are entered, the input is added to the sum, and the counter is increased by 1.

if you enter -1, which is invalid input, you don't update the sum, nor the counter.

what about the "ends with -1" bit from the problem question, does the user input the -1? if thats the case the loop should be set up so that the looping stops when user keys in -1?

Yes. A loop will keep checking whether the input number is equal to -1. If it is, exit the loop. Also, right after you accept user input, you must check whether the number is valid (>=0 and <=100). If it is not valid, you could simply use continue; to go back to the start of the loop again.

this is what i have:

import java.util.Scanner;

public class meanAndMax {

    public static void main(String[] args) {
        //create/initialize scanner
        Scanner sc = new Scanner(System.in);
        int counter = 0;
        double input = 0, result = 0, average = 0, max = 0;

        do{
            System.out.println("Please enter the marks : ");
            input = sc.nextDouble();

            if(input >= 0 && input <= 100){
                result += input;
                counter++;
            }
        }while(input != -1);

        average = result/counter;

        for(int i=0; i<input; i++){
            if(input > max){
                max = input;
            }

        }

        System.out.println("Your results are:" + result);
        System.out.println("Highest number is " + max);
        System.out.println("The mean is " + average);

        sc.close();
    }
}

i got the counter down, right? now i need to find highest number. I just dont know how to find highest number. Not doing it right.

for(int i=0; i<input; i++){
            if(input > max){
                max = input;
            }
        }

this makes no sense. no sense at all. first of all: input is the input, not the number of inputs, that would be counter. secondly, input is a single value, not an array or a list, so it doesn't contain different values.

what you want(ed) to do, was put this test in your original iteration:

step 1: you declare max as -1 before you start your do-while. -1, because you can verify later on, that if (counter == 0 || max == -1), the user didn't enter valid input.

step 2: compare input to max after you just read it.

 if(input >= 0 && input <= 100){
            result += input;
            // here you add the 
            if(input > max){
                max = input;
            }
            counter++;
        }

step 3: only print the results if there are valid results to print:

if ( counter == 0 || max == -1){ // I leave it to you to explain why the second check is redundant
    System.out.println("No valid input entered.");
} else{
    System.out.println("Your results are:" + result);
    System.out.println("Highest number is " + max);
    System.out.println("The mean is " + average);
}

yeah i kind figured that that didnt make sense. I wasnt sure where to place it and if i needed to loop through the inputs (i was thinking about arrays). Anyway, thank you guys for the help! much appreciated. (Wonder if there is a way to change the title).

Interesting...
This exact requirement can be handled without storing all the input values, so it doesn't need an array (or list). What's interesting is what happens when the next exercise says "... and print all the values that are more than 2 standard deviations from the mean" (or any other output that requires you to know the total/average/whatever before processing each value)

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.