Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Comparing Elements in Array to find the Highest Int

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Comparing Elements in Array to find the Highest Int

    So in this program, which is a grading program, I am trying to compare all the students averages to find who has the highest one and list the grades and the student's names from least to greatest. Yes, I see there are other problems in the program but it is nowhere near finished.


     
     
    import java.util.Scanner;
     
    public class Main {
    	public static void main(String[] args) {
    		String[] studentName = new String[20];
    		int[] studentAverage = new int[20];
    		Scanner input = new Scanner(System.in);
    		int numberOf = 0;
    		int sub = 0;
    		int x = 0;
    		int[] answers;
    		int[] correct;
    		int whichStudent = 0;
    		int timesRan = 0;
    		String AvToOr, letterGrade, stuNameAsk, goOn;
    		while (x == 0) {
    			whichStudent = sub;
     
    			System.out
    					.println("Enter student name. (Current limit of amount of students is 20.)");
    			stuNameAsk = input.next();
    			studentName[sub] = stuNameAsk;
     
    			System.out
    					.println("How many questions would you like to ask? (Maximum is infinite.)");
    			int num = input.nextInt(); // get the number of times to ask
    			answers = new int[num]; // student's answers
    			correct = new int[num]; // for teachers
    			for (int x1 = 0; x1 < num; x1++) {
    				System.out.println("Enter the answer for question number: "
    						+ (x1 + 1) + ".");
    				correct[x1] = input.nextInt();
    			}
    			System.out.println("Now for the students.");
    			for (int x1 = 0; x1 < num; x1++) {
    				System.out
    						.println("Enter the student answer for question number:"
    								+ (x1 + 1));
    				answers[x1] = input.nextInt();
    			}
    			int average = 0; // his/her grade
    			for (int x1 = 0; x1 < num; x1++)
    				if (correct[x1] == answers[x1])
    					average++;
     
    			average = (int) (((double) average) / ((double) num) * 100);
    			if (average >= 90 && average <= 100) {
    				letterGrade = "A";
     
    			} else if (average >= 80 && average <= 89) {
    				letterGrade = "B";
    			} else if (average >= 70 && average <= 79) {
    				letterGrade = "C";
    			} else if (average >= 60 && average <= 69) {
    				letterGrade = "D";
    			} else {
    				letterGrade = "F";
    			}
    			studentAverage[sub] = average;
    			System.out.println(studentName[0 + whichStudent] + "'s grade is "
    					+ studentAverage[0 + whichStudent] + "%");
    			System.out
    					.println("Do you want to enter another student? Type Y for yes and N for no.");
    			goOn = input.next();
     
    			if (goOn.equalsIgnoreCase("n")) {
    				x++;
     
    			} else if (goOn.equalsIgnoreCase("y")) {
    				sub++;
    				timesRan++;
     
    			} else {
    				System.out
    						.println("Your answer is not valid. Please try again.");
    				System.out.println("Do you want to enter another student?");
    				goOn = input.next();
     
    				if (goOn.equalsIgnoreCase("n")) {
    					x++;
     
    				} else if (goOn.equalsIgnoreCase("y")) {
    					sub++;
    					timesRan++;
     
    				} else {
    					System.out
    							.println("Your answer is not valid. Program restarted.");
    				}
    			}
    		}
    		System.out.println("All entered students and their averages:");
    		for (int varTest = 0; varTest <= timesRan; varTest++) {
    			System.out.println(studentName[varTest] + "'s average is "
    					+ studentAverage[varTest] + "%");
    		}
    Here is the specific place in which I'm trying to put the grades in order from highest to lowest...
    		System.out
    				.println("Would you like to (A)verage, put in (or)der from highest grade to lowest grade, or (end) the program? (Case does not matter)");
    		AvToOr = input.next();
     
    		 if	 (AvToOr.equalsIgnoreCase("A")){
    			for (int varTest = 0; varTest <= timesRan; varTest++){
    				//System.out.println(studentAverage[varTest] + studentAverage[varTest-1]);
    			}
     
    			}
    		 else if (AvToOr.equalsIgnoreCase("top")){
    				System.out.println("Test");
    		}
    		 else if (AvToOr.equalsIgnoreCase("or")){
    			 System.out.println("test");
    		 }
    		 else if (AvToOr.equalsIgnoreCase("end")){
     
    		 }
     
    	}
    }
    Thanks


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Comparing Elements in Array to find the Highest Int

    Do you have a question?

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Comparing Elements in Array to find the Highest Int

    Quote Originally Posted by Cornix View Post
    Do you have a question?
    I am trying to compare all the students averages to find who has the highest one and list the grades and the student's names from least to greatest. I need help with this.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Comparing Elements in Array to find the Highest Int

    Ok.
    Can you explain where you are having problems? Maybe ask a specific question that some one can help you with.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Comparing Elements in Array to find the Highest Int

    Quote Originally Posted by Norm View Post
    Ok.
    Can you explain where you are having problems? Maybe ask a specific question that some one can help you with.
    I don't know what i would have to do to compare multiple array elements against each other.

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Comparing Elements in Array to find the Highest Int

    Have you ever discussed a sorting algorithm in class? Because they are quite similar to this problem, perhaps you might re-use some of the thoughts from the sorting algorithm.
    If not, then think how you would do it by hand, on a piece of paper perhaps.
    Write down a list of possible entries and then think, step by step, how you would find the highest value from those entries by hand.

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Comparing Elements in Array to find the Highest Int

    Quote Originally Posted by Cornix View Post
    Have you ever discussed a sorting algorithm in class? Because they are quite similar to this problem, perhaps you might re-use some of the thoughts from the sorting algorithm.
    If not, then think how you would do it by hand, on a piece of paper perhaps.
    Write down a list of possible entries and then think, step by step, how you would find the highest value from those entries by hand.
    Thanks for the help! This is the code I wrote to do it....
    for (int index = 0; index <= timesRan; index++)
    			{
    				if (studentAverage[index] > highest)
    				highest = studentAverage[index];
     
     
    			}
    			for(int tester = 0; tester <= timesRan; tester++){
    				if (studentAverage[tester]==highest){
    					topStudent = studentName[tester];
    					tester = timesRan++;
    				}
    				else {
     
    				}
    			}
    			System.out.println(highest +"%"+ " was the highest grade by " + topStudent);
    		} else if (AvToOr.equalsIgnoreCase("end")) {
     
    		}

  8. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Comparing Elements in Array to find the Highest Int

    Do you have any more questions or problems?

Similar Threads

  1. Printing highest number of array. Can't find my error.
    By Praetorian in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2013, 11:46 PM
  2. Replies: 0
    Last Post: January 22nd, 2013, 10:15 PM
  3. Counting and comparing elements in an array
    By TheBestGame in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 31st, 2012, 05:05 AM
  4. Comparing similar elements in an array
    By FJIW in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 25th, 2011, 10:22 AM
  5. Comparing elements of an arrayList with an array?
    By DudeJericho in forum Collections and Generics
    Replies: 2
    Last Post: April 21st, 2011, 12:39 PM