I am coding a JAVA project and need help comparing two string arrays. One array
ID: 3676930 • Letter: I
Question
I am coding a JAVA project and need help comparing two string arrays. One array is two dimensional and has the Student ID number in the first column and their test answers in the second column. It looks like this:
819283761, 2112211241
834987341, 2221213241
899991828, 1122113241
888181818, 2121113131
The second array has the test answers in it. It looks like this:
1122113241
2221213241
1122113241
2121113131
1111111111
I need to compare the students answers with the test answers and then give back the number of questions they got correct.
Explanation / Answer
program:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MDR
*/
import java.io.*;
public class TestAnswers {
public static void main(String []args)throws IOException
{
String[][] StudentAns={{"819283761", "2112211241"},
{"834987341", "2221213241"},
{"899991828", "1122113241"},
{"888181818", "2121113131"}
};
String[] TestAns={"1122113241",
"2221213241",
"1122113241",
"2121113131",
"1111111111"};
int count=0;
for(int i=0;i<4;i++)
{
System.out.println("Student id:"+StudentAns[i][0]);
for(int j=0;j<5;j++)
{
count=0;
for(int k=0;k<10;k++)
{
if(StudentAns[i][1].charAt(k)==TestAns[j].charAt(k))
{
count++;
}
}
System.out.println("Test"+(j+1)+"result:"+count);
}
}
}
}
output:
run:
Student id:819283761
Test1result:6
Test2result:6
Test3result:6
Test4result:4
Test5result:5
Student id:834987341
Test1result:6
Test2result:10
Test3result:6
Test4result:6
Test5result:3
Student id:899991828
Test1result:10
Test2result:6
Test3result:10
Test4result:6
Test5result:5
Student id:888181818
Test1result:6
Test2result:6
Test3result:6
Test4result:10
Test5result:6
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.