We have this advance assignment about 2Dimensional array. The problem goes like this. Program that will accept names and score. Store the data on a 2 dimensional array. Then display the list of students plus its score and get the highest score.
Since our instructor teach us basic on array i don't have much idea about this 2D array I already do a lot of research but still i cant get the exact codes i need.
Below is my unfinished codes. My problem so far is that i can't print all the names and scores. it only prints the last data entered.
Any help will do tnx.
int[] studlist;
String name="";
String fscore="";
int i, score;
Scanner myScanner = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many students?:");
int students = myScanner.nextInt();
studlist = new int[students];
for( i = 0; i < studlist.length; i++){
try{
System.out.println("Enter Name:");
name = br.readLine();
}
catch(IOException e)
{
System.out.println("Error enter name!");
}
try{
System.out.println("Enter Score:");
fscore = br.readLine();
}
catch(IOException f)
{
System.out.println("Error enter grade!");
}
}
System.out.println("Display list of Students and Score:");
System.out.println(name+" "+fscore);
}
}
Create your 2 dimensional array like
String[][] arr2d = new String[students][2];
.
In your loop, you can use
arr2d[i][0] = name;
and
arr2d[i][1] = fscore;
To check the highest score, iterate over your array and compare the score fields( arr2d[i][1]
).
Two dimensional arrays are referred to as array of arrays in Java because there exist no 2D array in Java. Why? because if 2D array then they should be stored in consecutive memory locations. Read here on 2D arrays. Read here on how 2D arrays or array of arrays work.
String[][] studentList = new String[10][2];
First field represents number of rows and the second represents the number of columns in your case. So above is going to take 10 student array where each of those student arrays have two elements[0,1].
I have developed a sample code example, to help you understand arrays, how to use try and catch, exception and loop.
public static void main(String[] args) {
//your 2D array or array of arrays def
String[][] studentList = null;
Scanner input = new Scanner(System.in);
//temporary students count
int students = 1;
// i is used to take number of students, -1 to initialize do while loop
int i = -1;
do {
try {
//executed only once
if (i == -1) {
System.out.println("Enter the number of students : ");
// if you provide anything but integer, this throws
// InputMisMatchException so validate if
// Also validate against negative integer input
students = input.nextInt();
// move the input cursor to beginging to line
// this is because of using input.nextInt() before
input.nextLine();
//Initialize 2D array, make sure students is +ve
studentList = new String[students][2];
//increment i to 1 so above if condition fail
i++;
} else {
System.out.println("Enter Name for Student " + i + " :");
studentList[i][0] = input.nextLine();
System.out.println("Enter Score for " + studentList[i][0] + " : ");
studentList[i][1] = input.nextLine();
i++;
//all above inputs can reside inside one try/catch block
//no need for extra try/catch
}
} catch (Exception e) {
// all exceptions caught here, any type because Exception is
// parent of all others i.e. InputMisMatchException etc.
System.out.println("Error enter name!");
}
//loop breaks after n student is inputed
} while (i < students);
// close the input stream
input.close();
//making sure that student has been initialized and has student data
if (studentList != null && studentList.length > 0) {
System.out.println("The students are as follow: ");
// To display students yo need to iterate through them using loop
for (int j = 0; j < students; j++) {
System.out.println(studentList[j][0] + " : " + studentList[j][1]);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.