?
Solved

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

Posted on 2011-10-02
14
Medium Priority
?
542 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:Member_2_4213139
  • 6
  • 3
  • 3
  • +2
14 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 36900716
You need to declare the array *before* the loop and fill it *in* the loop
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36900726
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
 
LVL 47

Expert Comment

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

Open in new window


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 36900761
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36900764
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36900766
then you want to sort it after you populate the whole array
just before printing

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36900796
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
 

Author Comment

by:Member_2_4213139
ID: 36900854
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
 
LVL 47

Accepted Solution

by:
for_yan earned 1000 total points
ID: 36900863
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
 

Author Comment

by:Member_2_4213139
ID: 36903296
Much better than my original plan!  THANK YOU!!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36903314
ryanasalazar, can you tell me why you ignored my comment please?
0
 

Author Comment

by:Member_2_4213139
ID: 36905232
@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
 
LVL 10

Assisted Solution

by:gordon_vt02
gordon_vt02 earned 500 total points
ID: 36906869
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
 

Expert Comment

by:South Mod
ID: 36923354
Re-awarded points after Zone Advisor review.

SouthMod
EE Moderator
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question