Flamur Kelmendi Flamur Kelmendi - 1 month ago 16
Java Question

Java program will not recognize sentinel value

My program accept input data from a user (up to 20 values) and calculate the average/find the distance from the average. If the user enters "9999" when no numbers have been added yet it will display an error message and tell the user to re-enter a value. Otherwise entering "9999" will collect what the user has entered and do its calculations. My program will have to collect all 20 inputs from the user and also ignore when the value "9999" is entered completely but, it will do the other calculations correctly. I'm not sure why its not recognizing my sentinel value whatsoever.

package labpack;

import java.util.Scanner;

public class Lab4 {

public static void main(String[] args) {
int i = 0;
double [] numbers = new double[20];
double sum = 0;
int sentValue = 9999;

java.util.Scanner input = new java.util.Scanner(System.in);

System.out.print("Enter the numbers you want up to 20");

do {
for (i = 0; i < numbers.length; i++) {
if (numbers[0] == sentValue){
System.out.println("Error: Please enter a number");
break;
}
else {
numbers[i] = input.nextDouble();
sum += numbers[i];
}
}

while (i<numbers.length && numbers[i]!=sentValue); //part of do-while loop

//calculate average and distance from average
double average = (sum / i);
System.out.println("This is your average:" + average);

for (i = 0; i < numbers.length; i++) { //Display for loop
double diffrence = (average-numbers[i]);
System.out.println("This is how far number " +numbers[i] +" is from the average:" + diffrence);
}
}
}

Answer

You can do this without doing the do-while and doing while instead.

if (numbers[0]== sentValue){
            System.out.println("Error: Please enter a number");
            break;

Here you are trying to compare the value without initializing the array with the user input.

This can be done in a much simple way :

import java.util.Scanner;
public class Lab4 {

    public static void main(String[] args) {
        int i = 0;
        double [] numbers =new double[10];
        double sum =0;
        double sentValue=9999;
        int count = 0;
        System.out.println(numbers.length);
        System.out.print("Enter the numbers you want up to 20");
        Scanner input = new Scanner(System.in);
        while (i<numbers.length){
            double temp = input.nextDouble();
            if (temp >= sentValue){
                if(i==0){
                    System.out.println("Error Message Here");
                } else {
                    break;
                }
            }//if
            else {
                numbers[i] = temp;
                sum += numbers[i];
                i++;
                count++;
            }
        } //part of while loop*/

        //calculate average and distance from average
        double average=(sum/i);
        System.out.println("This is your average:" + average);

        for (i=0;i < count;i++){ //Display for loop
            double diffrence = (average-numbers[i]);
            System.out.println("This is how far number " +numbers[i] +" is from the average:" + diffrence);
        }//for loop
    }//main bracket

}//class lab4 bracket

You need to store the value of the input.nextDouble() into a variable because when the compiler reads input.nextDouble(), each time it will ask the user for an input.

PS. You dont need to re-initialize this part :

java.util.Scanner input = new java.util.Scanner(System.in);

The above line can simply be written as :

Scanner input = new Scanner(System.in);

because you already imported Scanner.

import java.util.Scanner;

Hope this helps :)

Comments