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 11 of 11

Thread: Reusing an Entered List of Array Elements

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

    Default Reusing an Entered List of Array Elements

    If you see in the code below where I print "Would you like to use previous key" I need to be able to save the previous key answers to be used again.. not sure how to go about doing this..
    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 highest = 0;
    		int averageHolder = 0;
    		String[] answers;
    		String[] correct;
    		int whichStudent = 0;
    		int timesRan = 0;
    		String AvToOr, letterGrade, stuNameAsk, goOn, sameKey, topStudent = null, keyRem;
    		while (x == 0) {
    			whichStudent = sub;
    			if (timesRan >= 1) {
    				System.out
    						.println("Would you like to use the same key as the previous one entered? Y for yes and N for no (Case does not matter).");
    				keyRem = input.next();
    				if (keyRem.equalsIgnoreCase("y")) {
    					System.out.println();
    				} else if (keyRem.equalsIgnoreCase("n")) {
     
    				}
    			}
    			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 input? (Maximum is infinite.)");
    			int num = input.nextInt(); // get the number of times to ask
    			answers = new String[num]; // student's answers
    			correct = new String[num]; // for teachers
    			for (int x1 = 0; x1 < num; x1++) {
    				System.out
    						.println("Enter the answer for question number: "
    								+ (x1 + 1)
    								+ ". Answer types are A,B,C,D,E, etc. Case does not matter.");
    				correct[x1] = input.next();
    			}
    			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.next();
    			}
    			int average = 0; // his/her grade
    			for (int x1 = 0; x1 < num; x1++)
    				if (correct[x1].equalsIgnoreCase(answers[x1]))
    					average++;
     
    			average = (int) (((double) average) / ((double) num) * 100);
     
    			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("");
    			System.out.println(studentName[varTest] + "'s average is "
    					+ studentAverage[varTest] + "%");
    		}
    		System.out.println("");
    		System.out
    				.println("Would you like to (A)verage, find the (top) grade, or (end) the program? (Case does not matter)");
    		AvToOr = input.next();
     
    		if (AvToOr.equalsIgnoreCase("A")) {
    			for (int varTest = 0; varTest <= timesRan; varTest++) {
    				averageHolder = averageHolder + studentAverage[varTest];
     
    			}
    			System.out.println(averageHolder / (timesRan + 1) + "%"
    					+ " is the average of all the students entered.");
    		} else if (AvToOr.equalsIgnoreCase("top")) {
    			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(topStudent + " had the highest grade with a "
    					+ highest + "%");
    		} else if (AvToOr.equalsIgnoreCase("end")) {
     
    		}
     
    	}
    }


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

    Default Re: Reusing an Entered List of Array Elements

    If you want to save something use a variable.

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

    Default Re: Reusing an Entered List of Array Elements

    Quote Originally Posted by Cornix View Post
    If you want to save something use a variable.
    But how do I use a variable for when the amount of questions in the key will never be constant?

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

    Default Re: Reusing an Entered List of Array Elements

    What exactly is this "key" you are talking about? Perhaps you should create a class for that or an interface.

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

    Default Re: Reusing an Entered List of Array Elements

    Quote Originally Posted by Cornix View Post
    What exactly is this "key" you are talking about? Perhaps you should create a class for that or an interface.
    The program is a grading program. The teacher enters the "key" which are the correct answers for the assignment, test, quiz, etc. then the students answers are entered and they are compared to get the students grade.

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

    Default Re: Reusing an Entered List of Array Elements

    But what is it? A number? A string? A complex object with several attributes?

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

    Default Re: Reusing an Entered List of Array Elements

    Quote Originally Posted by Cornix View Post
    But what is it? A number? A string? A complex object with several attributes?
    Its a String array.
    If you ran my program or at least read it entirely it might help you a little...

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

    Default Re: Reusing an Entered List of Array Elements

    I am not going to read 50+ lines of code just to find that out. If the key is a string array, why are you not able to save it in a variable?

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

    Default Re: Reusing an Entered List of Array Elements

    and then how would I put those variables into play?

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

    Default Re: Reusing an Entered List of Array Elements

    What do you want to use them for?

  11. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Reusing an Entered List of Array Elements

    String[] key = {"A", "D", "B"};
    String[] answers = {"A", "C", "B"};
    loop {
        if answer equals key element {
            inc count
        }
    }
    if use a new key {
        get new key
    }
    From the above psuedocode I don't see why using the same key would be a problem. You simply don't change any values in the key array.
    Improving the world one idiot at a time!

  12. The Following User Says Thank You to Junky For This Useful Post:

    jakeburns (September 15th, 2014)

Similar Threads

  1. deleting all linked list elements using ADT
    By memo1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 25th, 2011, 02:01 PM
  2. In a class create an array list of elements of another class, help!
    By LadyBelka in forum Collections and Generics
    Replies: 3
    Last Post: May 4th, 2011, 05:00 PM
  3. Reusing Panel code in each tabs on a tabbed pane
    By kurt-hardy in forum Object Oriented Programming
    Replies: 4
    Last Post: March 23rd, 2011, 08:21 AM
  4. Replies: 1
    Last Post: December 4th, 2010, 05:26 PM
  5. Shuffling elements in a linked list.
    By xecure in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 1st, 2010, 01:25 PM