-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLA1.java
More file actions
82 lines (74 loc) · 2.44 KB
/
LA1.java
File metadata and controls
82 lines (74 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package edu.wmich.cs1120.LA1Lee;
import java.util.Scanner;
import java.io.*;
//main class
public class LA1 {
public static void main(String args[]) throws FileNotFoundException{
try {
Scanner scanFile =new Scanner(new File ("mcq.txt"));
Scanner input=new Scanner(System.in);
String data="";
boolean flag=true;
System.out.println("File found!\n");
print();
System.out.println("If you are ready, press 1 (Press any numbers to end)");
if(input.nextInt()!=1) {
flag=false;
}
if (flag==true) {
try {
while(scanFile.hasNextLine()) {
data= data + scanFile.nextLine()+ " ";
}
scanFile.close();
String splited[]=data.split(" ");
for(int i=0;i<Integer.parseInt(splited[2]);i++){
if(Integer.parseInt(splited[2])!=(splited.length-3)) {
flag=false;
break;
}
else {
if(Integer.parseInt(splited[0])!=splited[i+3].length()) {
flag=false;
break;
}
}
}
if((Integer.parseInt(splited[0])==splited[1].length())&&(flag==true)) {
MultipleChoiceQuestion mcq =new MultipleChoiceQuestion(toArray(splited[1]),to2DArray(splited));
mcq.printAll();
}
else {
System.out.println("Error!!!");
print();
}
}
catch(NumberFormatException e){
System.out.println("Error!!!");
print();
}
}
}
catch(Exception e) {
System.out.println("Error!!!\nNote: Make sure the path of the file to be read is correct in the computer");
}
System.out.println("\n\nEnd.Shutting down...");
}
public static char[] toArray(String split) {
char ans[]=split.toCharArray();
return ans;
}
public static char[][] to2DArray(String splited[]){
char [][] student = new char[splited.length-3][splited[3].length()];
for(int i=3;i<splited.length;i++) {
for(int j=0;j<splited[i].length();j++) {
student[i-3][j]=splited[i].charAt(j);
}
}
return student;
}
public static void print() {
System.out.println("Please make sure your file is in the correct format\n(number of questions)\n(answer key)\n(numberof students)\n(the answer from each students line by line)");
System.out.println("Note:\n1)Make sure the number of question is the same for all the data given\n2)The number of students and the number of students' answer is the same");
}
}