In: Computer Science
Write a java code snippet that allows a teacher to track her students’ grades. Use a loop to prompt the user for each student’s name and the grade they received. Add these values to two separate parallel ArrayLists. Use a sentinel to stop. Output a table listing each of the student’s names in one column and their associated grades in the second column.
Here I use two separate List for store name and grade.
names is use for store name.
grades is use for store grade.
For creating list in java
import java.util.List;
List<String> names = new ArrayList<String>(); //use for store name
List<String> grades = new ArrayList<String>();//use for store grade
Here string is a type of list.
Printf() is use for formatting the output in table form.
code:
import java.util.*;
public class studentGrades{
public static void main(String[] args) {
String f = "y"; //Flag for next Entry
Scanner sc = new Scanner(System.in);//use for taking input
List<String> names = new ArrayList<String>(); //use for store name
List<String> grades = new ArrayList<String>();//use for store grade
while(f.equals("y")){//check condition for continue
String name,grade;
System.out.print("\nEnter Student name: ");
//taking name from user
name = sc.next();
System.out.print("Enter Student grade: ");
//taking grade from user
grade = sc.next();
//add name into list
names.add(name);
//add grade into list
grades.add(grade);
//ask user for continue enter name and grade or not
System.out.print("\nAre you want to continue?(y/n): ");
f = sc.next();
}
//formating table
System.out.println("\n-------------------------------------");
System.out.printf("%10s %20s", "NAME", "GRADE");
System.out.println();
System.out.println("-------------------------------------");
for(int i = 0; i < names.size();i++){
//print name and grade from the list
System.out.printf("%10s %20s\n", names.get(i), grades.get(i));
}
}
}
Output:
I hope this solution will help you.
If you have any doubts about this, then do comment below.
Do you feel it is helpful to you?
Then please up vote me.
Thank You :)