11 Replies - 450 Views - Last Post: 29 November 2012 - 07:44 PM Rate Topic: -----

#1 ValeriePurvis  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 78
  • Joined: 21-September 12

Look up in a double array using a loop - JAVA

Posted 27 November 2012 - 10:50 AM

This is NOT Homework any longer, it's been graded and now I would like someone to tell me the correct way to do a look up using a loop and a double array. The professor marked almost 50 percent off because I used the definately NOT recommended if/else statement format for this project. "array lookup for the parallel arrays; it uses a series of IF statements. There should be a loop. " is what she commented. I have re-read my manual and text and would just like to know what I was supposed to do, so that I can move on and be prepared for the next part by learning the error of my ways. Please find entire code below, the program was to use an array of students and 9 scores for each student, calculate the average, percent, and letter grade then print out the student name, avg, percent, and letter grade in a report. It does work, it's just not what she wants. I also lost points because I used the .sort and since it wasn't in the book apparently it wasn't allowed.... Thanks in advance...
//Valerie Purvis
//CIS 125

//This program is designed to read students names and scores from a .dat file
//The grades are sorted in ascending order and then the top 6 scores are selected.
//It then passes the student name and the percentage of the top 6 scores to the gradeReport()Method
//Once in the new Method, a letter grade is assigned to each percentage based on the elements of 
//the 2 parallel arrays ltrGrade and percentGrade.


import java.util.*;
import javax.swing.*;
import java.io.*;
public class StudentGrades
	{
	public static void main(String args[])throws Exception
  		{
		// Declare input file to be opened.
		FileReader fr = new FileReader ("scores.dat");//name of the .dat file.
		BufferedReader br = new BufferedReader (fr);
 		
		String studentName;
		String scoreString;
		int counter;
		double percent = 0;
		final int MAX_SCORE = 9; //number of grades for each student
		double scoresArray[] = new double[MAX_SCORE];
				
		
			System.out.println ("Student's Grade Report");
			System.out.println ("by:  Valerie Purvis");
			System.out.println ("");//blank
			System.out.println ("");  //lines
				
		while ((studentName = br.readLine()) != null)//Beginning of outer loop.
			{
			
			double average=0;
			double total=0 ;

				for (counter = 0; counter<MAX_SCORE; counter++)
					{
					 scoreString = (br.readLine());
					 scoresArray[counter] = Double.parseDouble(scoreString);
					 total+= scoresArray[counter];
					 average = (total/MAX_SCORE);
					}
				System.out.print ("Student Name:  "+studentName);
				System.out.printf ("     Avg. Score:  %.2f ",average);	
						int loopIndex;
						double MAX_TOTAL =600;
						//double percent = 0;
						double topScores = 0;
						Arrays.sort(scoresArray);// sorts the score values into default ascending order
																							
						for (loopIndex = 3; loopIndex < scoresArray.length ;loopIndex++)
						{	
							topScores += scoresArray[loopIndex];
						}
							percent = ((topScores/MAX_TOTAL)*100);
												
						// Sending studentName, and percent to gradeReports()method.
						gradeReports(studentName, percent);
				}
				
				
				br.close();	//closes the .dat file	
				System.exit(0);//End of main()method.
		}
		public static void gradeReports(String studentName, double percent)	//Begin gradeReports()Method
			{
			String ltrGrade[] =     {"A", "A-","B+","B", "B-","C+","C", "C-","D+","D", "D-","F"};  //parallel
			double percentGrade[] = {92.5,90.0,87.5,82.5,80.0,77.5,72.5,70.0,67.5,62.5,60.0,59.0} ;  //array
						
			System.out.printf ("     Percentage:  %5.1f%%  ", percent);

				if (percent>=percentGrade[0])
				{
					System.out.println ("     Grade:  " +ltrGrade[0]);
				}
				else if (percent>=percentGrade[1])
				{
					System.out.println ("     Grade:  "  +ltrGrade[1]);
				}
				else if (percent>= percentGrade[2])
				{
					System.out.println ("     Grade :  " +ltrGrade[2]);
				}
				else if (percent>=percentGrade[3])
				{
					System.out.println ("     Grade :" +ltrGrade[3]);
				}
				else if (percent>=percentGrade[4])
				{
					System.out.println ("     Grade :  " +ltrGrade[4]);
				}
				else if (percent>=percentGrade[5])
				{
					System.out.println ("     Grade :  " +ltrGrade[5]);
				}
				else if (percent>=percentGrade[6])
				{
					System.out.println ("     Grade :  " +ltrGrade[6]);
				}
				else if (percent>=percentGrade[7])
				{
					System.out.println ("     Grade :  " +ltrGrade[7]);
				}
				else if (percent>=percentGrade[8])
				{
					System.out.println ("     Grade :  " +ltrGrade[8]);
				}
				else if (percent>=percentGrade[9])
				{
					System.out.println ("     Grade :  " +ltrGrade[9]);
				}
				else if (percent>=percentGrade[10])
				{
					System.out.println ("     Grade :  " +ltrGrade[10]);
				}
				else if (percent<percentGrade[10])
				{
					System.out.println ("     Grade :  " +ltrGrade[11]);
				}
				
		}
			
}




text file of the data is attached.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Look up in a double array using a loop - JAVA

#2 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Look up in a double array using a loop - JAVA

Posted 27 November 2012 - 11:10 AM

well you can do it with one if and one else provided that you write a method to determine the index position of percentGrade that corresponds to percent

	System.out.println ("     Grade :  " +ltrGrade[checkCatogory(percent)]);					



int checkCatogory(double percent){
// loop through the percentGrade array to determine the corresponding index position
// you would need only one if else block here
}

This post has been edited by Crunch: 27 November 2012 - 11:20 AM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,887
  • Joined: 06-March 08

Re: Look up in a double array using a loop - JAVA

Posted 27 November 2012 - 11:23 AM

Wash !!! Understand your teacher :)

  for(int i = 0; i <= 10; ++i) {
     if(percent >= percentGrade[i]) { 
         System.out.println ("     Grade:  " +ltrGrade[i]);
         break;
     }
  }


Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7947
  • View blog
  • Posts: 13,531
  • Joined: 19-March 11

Re: Look up in a double array using a loop - JAVA

Posted 27 November 2012 - 11:44 AM

EDIT: oops. miscounted the array slots.

This is a really annoying way to structure your data!

This post has been edited by jon.kiparsky: 27 November 2012 - 11:46 AM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10766
  • View blog
  • Posts: 40,087
  • Joined: 27-December 08

Re: Look up in a double array using a loop - JAVA

Posted 27 November 2012 - 04:15 PM

And seriously avoid parallel arrays whenever possible. OOP is a much cleaner approach. :)
Was This Post Helpful? 0
  • +
  • -

#6 ValeriePurvis  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 78
  • Joined: 21-September 12

Re: Look up in a double array using a loop - JAVA

Posted 27 November 2012 - 09:07 PM

I have to do what the instructor wants. I stepped outside the box and used the .sort thing... she docked me big time for that one too! So I'm guessing the Check Category solution wouldn't be acceptable. @pbl, seriously was it that easy and I just DON"T understand my teacher? I hope that's all the harder it is... I know I made it much harder on myself than it had to be. But I did get it to work and give the output she wanted, I just didn't do it the way she wanted it done! @jon huh? Thanks for your input guys!
Was This Post Helpful? 0
  • +
  • -

#7 ValeriePurvis  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 78
  • Joined: 21-September 12

Re: Look up in a double array using a loop - JAVA

Posted 28 November 2012 - 07:05 PM

ok pbl, I succumb to your superiority! However, I am a bit baffled by the output now. In fact the output that I'm getting now is exactly why I went to the different if/else if long way around the turnip.

After each correct line it is listing the other letter grades. I'll post output at the bottom.

So I'm wondering, I have used the lowest number of the range between the scores, with the way I have it written, do I need to do the highest? (Except for the F which is the highest, because lowest would be 0)

//Valerie Purvis
//CIS 125

//This program is designed to read students names and scores from a .dat file
//The grades are sorted in ascending order and then the top 6 scores are selected.
//It then passes the student name and the percentage of the top 6 scores to the gradeReport()Method
//Once in the new Method, a letter grade is assigned to each percentage based on the elements of 
//the 2 parallel arrays ltrGrade and percentGrade.


import java.util.*;
import javax.swing.*;
import java.io.*;
public class StudentGrades2
	{
	public static void main(String args[])throws Exception
  		{
		// Declare input file to be opened.
		FileReader fr = new FileReader ("scores.dat");//name of the .dat file.
		BufferedReader br = new BufferedReader (fr);
 		
		String studentName;
		String scoreString;
		int counter;
		double percent = 0;
		final int MAX_SCORE = 9; //number of grades for each student
		double scoresArray[] = new double[MAX_SCORE];
				
		
			System.out.println ("Student's Grade Report");
			System.out.println ("by:  Valerie Purvis");
			System.out.println ("");//blank
			System.out.println ("");  //lines
				
		while ((studentName = br.readLine()) != null)//Beginning of outer loop.
			{
			
			double average=0;
			double total=0 ;

				for (counter = 0; counter<MAX_SCORE; counter++)
					{
					 scoreString = (br.readLine());
					 scoresArray[counter] = Double.parseDouble(scoreString);
					 total+= scoresArray[counter];
					 average = (total/MAX_SCORE);
					}
				System.out.print ("Student Name:  "+studentName);
				System.out.printf ("     Avg. Score:  %.2f ",average);	
						int loopIndex;
						double MAX_TOTAL =600;
						//double percent = 0;
						double topScores = 0;
						Arrays.sort(scoresArray);// sorts the score values into default ascending order
																							
						for (loopIndex = 3; loopIndex < scoresArray.length ;loopIndex++)
						{	
							topScores += scoresArray[loopIndex];
						}
							percent = ((topScores/MAX_TOTAL)*100);
												
						// Sending studentName, and percent to gradeReports()method.
						gradeReports(studentName, percent);
				}
				
				
				br.close();	//closes the .dat file	
				System.exit(0);//End of main()method.
		}
		public static void gradeReports(String studentName, double percent)	//Begin gradeReports()Method
			{
			String ltrGrade[] =     {"A", "A-","B+","B", "B-","C+","C", "C-","D+","D", "D-","F"};  //parallel
			double percentGrade[] = {92.5,90.0,87.5,82.5,80.0,77.5,72.5,70.0,67.5,62.5,60.0,59.0} ;  //array
						
			System.out.printf ("     Percentage:  %5.1f%%  ", percent);
			
			for (int i=0; i<=10;i++)
		
			 if(percent >= percentGrade[i])
				{
				 System.out.println ("     Grade:  "+ltrGrade[i]);
				}
				else if (percent<= percentGrade[11])
				 {
				  System.out.println ("     Grade:  " +ltrGrade[11]);
				 }
			
		}
	}
			



OUTPUT

Student's Grade Report
by: Valerie Purvis


Student Name: James Frank Avg. Score: 79.44 Percentage: 83.0% Grade: B
Grade: B-
Grade: C+
Grade: C
Grade: C-
Grade: D+
Grade: D
Grade: D-
Student Name: Betty Rice Avg. Score: 91.22 Percentage: 94.0% Grade: A
Grade: A-
Grade: B+
Grade: B
Grade: B-
Grade: C+
Grade: C
Grade: C-
Grade: D+
Grade: D
Grade: D-
Student Name: Carl Abbott Avg. Score: 70.22 Percentage: 73.8% Grade: C
Grade: C-
Grade: D+
Grade: D
Grade: D-
Student Name: Zane Zelazny Avg. Score: 61.33 Percentage: 68.7% Grade: D+
Grade: D
Grade: D-
Student Name: Mary Lane Avg. Score: 50.56 Percentage: 56.7% Grade: F
Grade: F
Grade: F
Grade: F
Grade: F
Grade: F
Grade: F
Grade: F
Grade: F
Grade: F
Grade: F
Was This Post Helpful? 0
  • +
  • -

#8 ValeriePurvis  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 78
  • Joined: 21-September 12

Re: Look up in a double array using a loop - JAVA

Posted 28 November 2012 - 09:38 PM

still working on the above. I changed the direction of the greaterthan/less than sign and I only get one F on the last student, however, I have discovered that if I write
for (int i=0; i<=10;i++)
	         
     if(percent >= percentGrade[i]|| percent < percentgrade[i+1])
      {
	System.out.println ("     Grade:  "+ltrGrade[i]);
      }
     else if (percent<= percentGrade[11])
	{
	System.out.println ("     Grade:  " +ltrGrade[11]); 
        }



It doesn't give me all the extra letter grades but it gives everyone a letter grade of F. So now I'm more confused. I would really like to know at this point what it is excactly that I am doing wrong!
Was This Post Helpful? 0
  • +
  • -

#9 ValeriePurvis  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 78
  • Joined: 21-September 12

Re: Look up in a double array using a loop - JAVA

Posted 29 November 2012 - 07:14 PM

View Postmacosxnerd101, on 27 November 2012 - 06:15 PM, said:

And seriously avoid parallel arrays whenever possible. OOP is a much cleaner approach. :)/>/>



Trust me I'd love to avoid them! But she's done it to us again with our final assignment. I know it shouldn't be this hard... and I don't know why she made us put 2 values in an array. It really doesn't make sense. But I do have a question for you about CompareTo.... and validation...

First I need to verify that a number is input and not anything else like letters for number of days. So that is something like:

int days;
String dayString;
days= Integer.parseInt (dayString);
while (days< MIN_DAYS)
   {
     dayString = JOptionPane.showInputDialog ("That entry is invalid.  Please Enter Number of Days you are Staying:  ");
     days = Integer.parseInt (dayString);
   }



Then I have to ask a question and ensure that it accepts Y,y, N,n and nothing else
In the loop I write for that do I only address one at a time, all at once or use something different to do this? As if they choose N then it calulates rate per night no meal plan and if Y is chosen it must ask another question " Which Meal Plan A or C?"


String question;

question = JOptionPane.showInputDialog("Do You Want a Meal Plan?  "Enter Y or N");
while (question.compareTo("Y")!=0) || (question.compareTo ("y")!=0)||(question.compareTo ("N") !=0) || (question.compareTo ("n") !=0)
   {
    question = JOptionPane.showInputDialog ("Invalid Entry. Please Enter Y or N");


This post has been edited by ValeriePurvis: 29 November 2012 - 07:16 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,887
  • Joined: 06-March 08

Re: Look up in a double array using a loop - JAVA

Posted 29 November 2012 - 07:22 PM

this will simply not work

days= Integer.parseInt (dayString);

it will throw an Exception and your code does not seem to be handling it
Was This Post Helpful? 0
  • +
  • -

#11 ValeriePurvis  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 78
  • Joined: 21-September 12

Re: Look up in a double array using a loop - JAVA

Posted 29 November 2012 - 07:28 PM

Hmm... It won't work because the code does not handle the exception, does that mean that it won't re-ask for the input or that the whole thing is incorrectly written and I need to change the way I think I am understanding the instructions?
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7947
  • View blog
  • Posts: 13,531
  • Joined: 19-March 11

Re: Look up in a double array using a loop - JAVA

Posted 29 November 2012 - 07:44 PM

Are you familiar with exception handling? If not, probably it would be good to read a bit of existing material on that, to save some typing.
As usual, the tutoracles are a pretty good starting point.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1