Quick (Simple?) Java Question. (in Off-topic)


SNK3R April 30 2006 8:41 PM EDT

The last part of my program is suppose to find the smallest number in the array, but I can't seem to comprehend why it's not giving me that. I've tried switching around greater than and lesser than signs, letting k = 1, but it won't budge. I'm asking for a little help. Don't just give the answer, tell me where I'm going wrong in my code. Hopefully it's something minute that I can change and not something I'm going to have to revamp.

P.S. Yes, I understand my array code is a bit unorthodox, so just roll with the punches. ;) Here's the code, but remember it's at the very end (color-coded):

import java.util.*;

public class GradeBook
{
	public static void main(String[] args)
	{
		Scanner keyboard = new Scanner(System.in);
		int grade, choice, index = 0, loop = 0, sum = 0;
		double average = 0;
		int[] gradebook = new int[11];

	while (loop == 0)
	{
		System.out.println("\n*****GradeBook Menu*****");
		System.out.println("  1.  Enter grade (-99 to return to menu)");
		System.out.println("  2.  Class average");
		System.out.println("  3.  Class Maximum");
		System.out.println("  4.  Class Minimum");
		System.out.println("  5.  Exit");
		System.out.println("");
		System.out.print("  Enter your choice:  ");

		choice = keyboard.nextInt();

		switch(choice)
		{
			case 1:
			System.out.println("You may only enter 10 grades:");
				grade = choice;

				while (grade != -99)
				{
					gradebook[index] = grade;
					sum = (gradebook[1] + gradebook[2] + gradebook[3] + gradebook[4] + gradebook[5] + gradebook[6] + gradebook[7] + gradebook[8] + gradebook[9] + gradebook[10]);
					index++;
					grade = keyboard.nextInt();
				}
				break;

			case 2:
				average = ((double)sum/(index-1));

				System.out.println("\n            >>>>>>>>>> The class average is: " + average + " <<<<<<<<<<\n");
				loop = 0;
				break;

			case 3:
				int maximum = gradebook[0], i;

				for (i = 0; i < gradebook.length; i++)
				{
					if (gradebook[i] > maximum)
					maximum = gradebook[i];
				}
				System.out.println("\n            >>>>>>>>>> The highest grade is: " + (double)maximum + " <<<<<<<<<<\n");
				loop = 0;
				break;

			case 4:
				int minimum = gradebook[0], k;

					for (k = 0; k < gradebook.length; k++)
					{
						if (gradebook[k] < minimum)
						minimum = gradebook[k];
					}
				System.out.println("\n            >>>>>>>>>> The lowest grade is: " + (double)minimum + " <<<<<<<<<<\n");
				loop = 0;
				break;

			case 5:
				System.out.println("\n\nGood-bye!\n\n");
				System.exit(0);
				break;

			default:
				System.out.println("\nHey, enter a valid option!\n");
				loop = 0;
				break;
		}
	}
}
}

Any help would be appreciated. It's got to be something I keep overlooking...

SNK3R April 30 2006 9:06 PM EDT

After discussing with Pit a few things, here's a revised edition (still no go on the minimum value [works with negative numbers], but maximum value works perfectly):

			case 3:
				int maximum = gradebook[0];

				for (int i = 1; i < gradebook.length; i++)
				{
					if (gradebook[i] > maximum)
					maximum = gradebook[i];
				}
				System.out.println("\n            >>>>>>>>>> The highest grade is: " + maximum + " <<<<<<<<<<\n");
				loop = 0;
				break;

			case 4:
				int minimum = gradebook[0];

					for (int i = 1; i < gradebook.length; i++)
					{
						if (gradebook[i] < minimum)
						minimum = gradebook[i];
					}
				System.out.println("\n            >>>>>>>>>> The lowest grade is: " + minimum + " <<<<<<<<<<\n");
				loop = 0;
				break;

SNK3R April 30 2006 9:10 PM EDT

Another interesting thing to note is that the minimum value function will work if I input negative numbers for the array in case 1.

SNK3R April 30 2006 9:59 PM EDT

Here's an updated version for Pit (and anyone else who is helping). In this version, I went back to traditional array-style. I figured out that since I was skipped over gradebook[0] and it was defaulting to 0, that was why I was getting 0 as a value for the minimum. Makes sense for both positive and negative numbers. Now I'm trying to fix my case 2 and case 4.

import java.util.*;

public class GradeBook
{
	public static void main(String[] args)
	{
		Scanner keyboard = new Scanner(System.in);
		int grade, choice, index = 0, loop = 0, sum = 0;
		double average = 0;
		int[] gradebook = new int[10];

	while (loop == 0)
	{
		System.out.println("\n*****GradeBook Menu*****");
		System.out.println("  1.  Enter grade (-99 to return to menu)");
		System.out.println("  2.  Class average");
		System.out.println("  3.  Class Maximum");
		System.out.println("  4.  Class Minimum");
		System.out.println("  5.  Exit");
		System.out.println("");
		System.out.print("  Enter your choice:  ");

		choice = keyboard.nextInt();

		switch(choice)
		{
			case 1:
			System.out.println("You may only enter 10 grades:");
				grade = choice;

				while (grade != -99)
				{
					gradebook[index] = grade;
					index++;
					grade = keyboard.nextInt();
				}
				break;

			case 2:
			for (int i = 0; i < index; i++)
			{
				sum += gradebook[i];
			}
				average = ((double)sum/(index-1));
				System.out.println("\n            >>>>>>>>>> The class average is: " + average + " <<<<<<<<<<\n");
				loop = 0;
				break;

			case 3:
				int maximum = gradebook[0];

				for (int i = 1; i < index; i++)
				{
					if (gradebook[i] > maximum)
					maximum = gradebook[i];
				}
				System.out.println("\n            >>>>>>>>>> The highest grade is: " + maximum + " <<<<<<<<<<\n");
				loop = 0;
				break;

			case 4:
				int minimum = gradebook[0];

				for (int i = 1; i < index; i++)
				{
					if (gradebook[i] < minimum)
					minimum = gradebook[i];
				}
				System.out.println("\n            >>>>>>>>>> The lowest grade is: " + minimum + " <<<<<<<<<<\n");
				loop = 0;
				break;

			case 5:
				System.out.println("\n\nGood-bye!\n\n");
				System.exit(0);
				break;

			default:
				System.out.println("\nHey, enter a valid option!\n");
				loop = 0;
				break;
		}
	}
}
}

SNK3R April 30 2006 10:17 PM EDT

Fixed case 2. Average and maximum works now, but minimum is still spitting a 1 out for positive inputs in the array.

SNK3R April 30 2006 10:47 PM EDT

Thanks to Pit and Karn, it's pretty much finished. Changed grade = choice; and got rid of it since it was already pre-registering a 1 in the grade (thanks, Karn!). Changed the array to 11 values so a user can enter 10 numbers, then -99 to get back to menu. Also, changed case 4 to read int minimum = gradebook[1];. It works fine like that, but not at gradebook[0], so I don't understand that. (Maybe someone can explain that.) I guess I was lucky to change it to a 1. ;) Here's the working code:

import java.util.*;

public class GradeBook
{
	public static void main(String[] args)
	{
		Scanner keyboard = new Scanner(System.in);
		int grade = 0, choice, loop = 0, index = 0, sum = 0, average = 0;
		int[] gradebook = new int[11];

	while (loop == 0)
	{
		System.out.println("\n*****GradeBook Menu*****");
		System.out.println("  1.  Enter grade (-99 to return to menu)");
		System.out.println("  2.  Class average");
		System.out.println("  3.  Class Maximum");
		System.out.println("  4.  Class Minimum");
		System.out.println("  5.  Exit");
		System.out.println("");
		System.out.print("  Enter your choice:  ");

		choice = keyboard.nextInt();

		switch(choice)
		{
			case 1:
			System.out.println("You may only enter 10 grades:");

				while (grade != -99)
				{
					gradebook[index] = grade;
					index++;
					grade = keyboard.nextInt();
				}
				break;

			case 2:
			for (int i = 0; i < index; i++)
			{
				sum += gradebook[i];
			}
				average = (sum/(index-1));
				System.out.println("\n                 >>>>>>>>>> The class average is: " + average + " <<<<<<<<<<\n");
				loop = 0;
				break;

			case 3:
				int maximum = gradebook[0];

				for (int i = 1; i < index; i++)
				{
					if (gradebook[i] > maximum)
					maximum = gradebook[i];
				}
				System.out.println("\n                 >>>>>>>>>> The highest grade is: " + maximum + " <<<<<<<<<<\n");
				loop = 0;
				break;

			case 4:
				int minimum = gradebook[1];

				for (int i = 1; i < index; i++)
				{
					if (gradebook[i] < minimum)
					minimum = gradebook[i];
				}
				System.out.println("\n                 >>>>>>>>>> The lowest grade is: " + minimum + " <<<<<<<<<<\n");
				loop = 0;
				break;

			case 5:
				System.out.println("\n\nGood-bye!\n\n");
				System.exit(0);
				break;

			default:
				System.out.println("\nHey, enter a valid option!\n");
				loop = 0;
				break;
		}
	}
}
}

Maelstrom April 30 2006 11:48 PM EDT

OK, I'm going off of your original code - the latest ones have "workarounds" in them that don't make much sense.

Here's the original case 1:

		case 1:
			System.out.println("You may only enter 10 grades:");
			grade = choice;

			while (grade != -99)
			{
				gradebook[index] = grade;
				sum = (gradebook[1] + ...
				index++;
				grade = keyboard.nextInt();
			}
			break;
The first problem is "grade = choice;" - you're setting a grade value to be "1", because that's the value of "choice" to get to the grade input. That's why your min value was returning "1".

The second problem is caused by the previous one: you don't set a value to the grades array for index=0 - you increment index without assigning a value at that position to the array. Instead of "grade = choice;" you should have "grade = keyboard.nextInt();" - so you ask for the grade.

That fixes your input problems, and also the problem where you were getting the value "-99" in your array. So case1 should be:

		case 1:
			System.out.println("You may only enter 10 grades:");
			grade = keyboard.nextInt();

			while (grade != -99)
			{
				gradebook[index] = grade;
				sum = (gradebook[1] + ...
				index++;
				grade = keyboard.nextInt();
			}
			break;
The other problem in your original code is the sentinel value you use in the for loops of cases 3 and 4. You corrected that in a later version of your code - you need to check the value of "index" and not "gradebook.length"

A couple of other things: when inputting values, you should check to make sure you still have room in the array before trying to add it - as it is, your code crashes if you try to add more than 11 values (and now that you've fixed the problem, you can change the array size back to 10).

The other thing I noticed is that you keep setting "loop=0;" but you never change the value of loop. Get rid of those statements. This brings us to case 5:

		case 5:
			System.out.println("\n\nGood-bye!\n\n");
			System.exit(0);
			break;
If you have the "System.exit(0);" statement, then you don't need the "break;" statement. Alternatively, if you want to actually use the "loop" variable, you could replace the "System.exit(0);" statement, with "loop=1" (and keep the "break;" statement).
This thread is closed to new posts. However, you are welcome to reference it from a new thread; link this with the html <a href="/bboard/q-and-a-fetch-msg.tcl?msg_id=001nFD">Quick (Simple?) Java Question.</a>