Why won't my mins and maxs print? Array trouble?

I was trying to use the Math Class and Min & Max Functions to get the mins and max's of the entered numbers, but thought that an array would be easier.  However, nothing is going into my array.  What am I doing wrong?

import java.util.*;
import java.lang.Math;

public class EnhancedTestScoreApp
{
    public static void main(String[] args)
    {
        // display operational messages
        System.out.println("Please enter test scores that range from 0 to 100.");
        System.out.println("To end the program enter 999.");
        System.out.println();  // print a blank line

        // initialize variables and create a Scanner object
        double scoreTotal = 0;
        int scoreCount = 0;
        int testScore = 0;
        int minScore = 0;
        int maxScore = 0;
        Scanner sc = new Scanner(System.in);

    	// get a series of test scores from the user
        while ( testScore <= 998 )
        {
            // get the input from the user
            System.out.print("Enter score: ");
        	testScore = sc.nextInt();
                if (testScore >= 999)
                    break;
        	// accumulate score count and score total
                if (testScore <= 100)
        	{
                    scoreCount += 1;
                    scoreTotal += testScore;
                    
                    int i;
                    int [] scoreArray = new int [100];
                    {
                        for (i = 0; i < scoreArray.length; i++)
                        scoreArray [i] = i+1;
                        }
                    Arrays.sort (scoreArray);
                    
                    minScore = scoreArray[0];
                    maxScore = scoreArray[scoreArray.length - 1];
                    }
                else if (testScore >= 101 | testScore <= 998)
                    System.out.println("Invalid Entry, Not Counted.");
                }
    	// display the score count, score total, and average score
    	double averageScore = scoreTotal / scoreCount;
    	String message = "\n" +
    	                 "Score count:   " + scoreCount + "\n"
    	               + "Score total:   " + scoreTotal + "\n"
		      + "Average score: " + averageScore + "\n"
                     + "Minimum score: " + minScore + "\n"
                    + "Maximum score: " + maxScore + "\n";
		System.out.println(message);
	}
}

Open in new window

Member_2_4213139Asked:
Who is Participating?
 
for_yanCommented:
while the data is biening enetered the progam will know what are the minimum and maximum values up to this current momemnt
when the user finishes enetring that will be minimum/maximum for the whol input list

so in the beginning 9before any input) you assign to minScore the maximum posible vlaue and to maxScore the minumum posiible value

say,

minScore = 100000;
maxScore = -100000;

then you got some input testScore
thsi is what you do:

if(testScore < minScore)minScore = testScore;
if(testScore > maxScore)maxScore = testScore;

and then you go back for the new input of testScore and again
go through these checks/assignements

In the end of all inputs your  minScore will be minimum of all inputNumbers and maxScorre - the mximimu of all input
 

0
 
CEHJCommented:
You need to declare the array *before* the loop and fill it *in* the loop
0
 
CEHJCommented:
Something like
import java.lang.Math;

import java.util.*;


public class EnhancedTestScoreApp {
    public static void main(String[] args) {
        // display operational messages
        System.out.println("Please enter test scores that range from 0 to 100.");
        System.out.println("To end the program enter 999.");
        System.out.println(); // print a blank line

        // initialize variables and create a Scanner object
        double scoreTotal = 0;
        int scoreCount = 0;
        int testScore = 0;
        int minScore = 0;
        int maxScore = 0;
        Scanner sc = new Scanner(System.in);
        int[] scoreArray = new int[100];

        // get a series of test scores from the user
        while (testScore <= 998) {
            // get the input from the user
            System.out.print("Enter score: ");
            testScore = sc.nextInt();

            if (testScore >= 999) {
                break;
            }

            // accumulate score count and score total
            if (testScore <= 100) {
                scoreCount += 1;
                scoreTotal += testScore;

                scoreArray[scoreCount] = testScore;
            } else if ((testScore >= 101) | (testScore <= 998)) {
                System.out.println("Invalid Entry, Not Counted.");
            }
        }

        Arrays.sort(scoreArray);

        minScore = scoreArray[0];
        maxScore = scoreArray[scoreArray.length - 1];

        // display the score count, score total, and average score
        double averageScore = scoreTotal / scoreCount;
        String message = "\n" + "Score count:   " + scoreCount + "\n" +
            "Score total:   " + scoreTotal + "\n" + "Average score: " +
            averageScore + "\n" + "Minimum score: " + minScore + "\n" +
            "Maximum score: " + maxScore + "\n";
        System.out.println(message);
    }
}

Open in new window

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
for_yanCommented:
you also need to assign testSScore to the array - not likt this:
 scoreArray [i] = i+1;

Open in new window


0
 
for_yanCommented:
this is tested and working

import java.util.*;
import java.lang.Math;

public class EnhancedTestScoreApp
{
    public static void main(String[] args)
    {
        // display operational messages
        System.out.println("Please enter test scores that range from 0 to 100.");
        System.out.println("To end the program enter 999.");
        System.out.println();  // print a blank line
                             int [] scoreArray = new int [100];
        // initialize variables and create a Scanner object
        double scoreTotal = 0;
        int scoreCount = 0;
        int testScore = 0;
        int minScore = 0;
        int maxScore = 0;
        Scanner sc = new Scanner(System.in);

    	// get a series of test scores from the user

        while ( testScore <= 998 )
        {
            // get the input from the user
            System.out.print("Enter score: ");
        	testScore = sc.nextInt();
            System.out.println("test: " +testScore);
                if (testScore >= 999)
                    break;
        	// accumulate score count and score total
                if (testScore <= 100)
        	{

                    scoreTotal += testScore;

                    int i;
                               System.out.println(scoreCount);
                    System.out.println(testScore);

                    //    for (i = 0; i < scoreArray.length; i++)
                        scoreArray [scoreCount] = testScore;
                 scoreCount += 1;


                    }
                else if (testScore >= 101 | testScore <= 998)
                    System.out.println("Invalid Entry, Not Counted.");
                }
    	// display the score count, score total, and average score

        for(int k: scoreArray)System.out.println(k);
        Arrays.sort (scoreArray,0, scoreCount);

             for(int k: scoreArray)System.out.println("after: " + k);


                    minScore = scoreArray[0];
                    maxScore = scoreArray[scoreCount-1];
    	double averageScore = scoreTotal / scoreCount;
    	String message = "\n" +
    	                 "Score count:   " + scoreCount + "\n"
    	               + "Score total:   " + scoreTotal + "\n"
		      + "Average score: " + averageScore + "\n"
                     + "Minimum score: " + minScore + "\n"
                    + "Maximum score: " + maxScore + "\n";
		System.out.println(message);
	}
}

Open in new window



Score count:   2
Score total:   20.0
Average score: 10.0
Minimum score: 5
Maximum score: 15

Open in new window

0
 
for_yanCommented:
the idea tio use array in this case was not the best ione - you
of course want to initilize it before everyhting

then you want to assign values to it

then you want to count number of your points ansd sort it only betwen 0 and this number

0
 
for_yanCommented:
then you want to sort it after you populate the whole array
just before printing

0
 
for_yanCommented:
another drwaback of arry is that you cannot know how many you'll have.

so in this cases it is better to do
if(testScore > maxScore) maxScore = testScore;
if(testScore < minScore) minScore = testScore;

it wull be much easier and shorter if you dot need to prserver your values for future

if you need to preserve the values then use ArrayList

0
 
Member_2_4213139Author Commented:
THANK YOU!  This is VERY helpful!!!  I think I like the non-array thing... however, while the data is entered, how will the program know which is min and which is max to put that score into the variables minScore/maxScore?
0
 
Member_2_4213139Author Commented:
Much better than my original plan!  THANK YOU!!!
0
 
CEHJCommented:
ryanasalazar, can you tell me why you ignored my comment please?
0
 
Member_2_4213139Author Commented:
@CEHJ:

MY BAD!  I am SO SORRY... that was MY mistake entirely!  I apologize for the slight!  As I was reading the comments, I missed that they were done by two different people!  Yan's solution was the best, although your advice of putting the array BEFORE the loop was also helpful!

Again, I;m sorry!!!
0
 
gordon_vt02Commented:
Also, be careful on this line:

else if (testScore >= 101 | testScore <= 998)

Open in new window


You're doing a bit-wise OR there instead of a boolean OR.  It should be || instead of |
0
 
South ModModeratorCommented:
Re-awarded points after Zone Advisor review.

SouthMod
EE Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.