3

This program will ask the user for students name and grade then displays both. The values that the user inputs are stored in array names & array grade. I use a counter controlled for loop to gather user input into the arrays. What if I wanted to enter multiple grades for each student?? Fairly new to programming, any input or thoughts would be greatly appreciated on how to do so...

public class Five {

public static void main(String[] args) {
    int students;

    Scanner input = new Scanner(System.in); //created input scanner


System.out.println("How many students are on your roster? If you wish to exit please type 00: ");// Initializing statement for program************
students = input.nextInt();

       if(students == 00) { //Exit program****************
       System.out.println("Maybe Next Time!!");
           System.exit(0);
         }

String[] names = new String[students];// Array names*******
String[]grade = new String[students]; //Array grade********

// Use Counter to go through Array**************************
        for(int counter =0; counter < students; counter++){
        System.out.println("Enter the name of a student: " +  (counter +1));
        names [counter] = input.next();
        System.out.println("Now enter that students grade A, B, C, D OR F: ");
        grade [counter] = input.next();
        }

input.close();// End Scanner object
//Use For loop for Printing names and grades entered by the user**************
System.out.println("Your students names and grades are as follows: ");
        for(int counter =0; counter < students; counter++){
        System.out.println("Name: " + names[counter]);
        System.out.println("Grade: " + grade[counter]);
        }
    }
}
3
  • for multiple grades, you will have to know the amount of them in advance since you are using arrays and they can only be initialized when you know the size in advance Commented Feb 13, 2016 at 7:18
  • 1
    Have you learned about Objects (POJOs) yet? A class where you find get and set methods (getters and setters)? Or tell me how many grades will you enter for each students? Like for each student there will be X grades. One for English, one for Physics, and one for Chemisty, etc. Commented Feb 13, 2016 at 7:21
  • 1
    It would be much better to use Classes and Objects or atleast HashMaps. Commented Feb 13, 2016 at 7:32

2 Answers 2

3

You could use a ragged array for the grades to enter more than one You would need to declare it so here is a way to do that.

    String[] names = new String[students];// Array names*******
    String[][]grade = new String[names][]; //Array grade********

      for(int i=0; i<names[i];i++)
  {System.out.println("How many grades will you enter for "+names[i]+"?")
   int temp=input.nextInt();
  for(int j=0; j<names[i][temp-1];j++){
  grad[i][j]=input.next();         
           }}
Sign up to request clarification or add additional context in comments.

1 Comment

Does this allow for more than 2 grades? What if you didn't know the amount of grades each student was going to have. I'm trying to think real world.
2

You should probably make a Student class and a Grades class and store them as objects. Using a data structure your best choice is a .

HashMap<String, List<String>> grades = new HashMap<>();
grades.put("Jack", new ArrayList<>());
grades.get("Jack").add("A");

You can find out more about HashMap here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.