mga lods ano error ko dyan diko masolve eh
Java:
package labexer5b;
import java.lang.Exception;
import java.util.Scanner;
class NumberOrSpecialCharacterException extends Exception { //Custom Exception
public NumberOrSpecialCharacterException() {
super("[Special Characters or Numbers is not allowed.]");
}
}
class ExceedLetterException extends Exception { //Custom Exception
public ExceedLetterException() {
super("[Choices are only A , B , C.]");
}
}
public class LabExer5B {
public static Scanner Input = new Scanner(System.in); //Main Variables
public static String Enter;
public static int Block = 1;
public static int Page = 1;
private int Score = 0;
public static boolean WhileStop = true;
public static String ChoicesAndQuestions[][][] = { { //Uses 3D Array for easy accesible
{"1. What is the Brain or Heart of a Computer?"}, // Questions
{"2. Which is a small device used to save and store computer files?"},
{"3. Who is the one of the Inventor of Microsoft Windows? "},
{"4. What is example of Input Device?"},
{"5. What is primary storage of computer? "},
{"6. Windows 3 was released in which year?"},
{"7. What cable does an Ethernet port or Lan port need?"},
{"8. Can computer still boot without storage drive? "},
{"9. Can you run an Operating System to a flash drive?"},
{"10.What Animal makes the sound woof?"}
},
{
{"A. CPU","B. APU","C. Processor"}, // Multiple Choices
{"A. USB","B. Flash Drive","C. Optical Drive"},
{"A. Paul Allen","B. Linus Torvalds","C. Steve Jobs"},
{"A. Cat","B. Dog","C. Mouse"},
{"A. RAM","B. Hard Disk Drive","C. Flash Drive"},
{"A. 2021","B. 1961","C. 1991"},
{"A. Cat","B. Dog","C. Mouse"},
{"A. Yes","B. No","C. Maybe"},
{"A. Yes","B. No","C. Maybe"},
{"A. Cat","B. Dog","C. Mouse"},
{"A","B","C"} // Checker OR Choices.
}
};
public void SetScore(int Score){ // Setter and Getter for Private Score
this.Score = Score;
}
public int GetScore() {
return Score;
}
public static int IsNumber(String a) { // Check if numeric is string uses try catch
try {
Integer.parseInt(a);
return 1;
}
catch (NumberFormatException e) {
return 0;
}
}
public static boolean CheckAlphabetic(String s) { //check if letter is include in alphabet
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!(c >= 'A' && c <= 'Z') && !(c >= 'a' && c <= 'z')) {
return false;
}
}
return true;
}
public static boolean AnswersChecker(String Enter) { //Return boolean value this method helps minimize code lines
if (CheckInput(Enter) == true) {
return true;
}
else if (CheckInput(Enter) == false) {
System.out.println("Do you want to try again? (yes) or (no)");
Enter = Input.nextLine();
if (Enter.equalsIgnoreCase("yes") || Enter.equalsIgnoreCase("y")) {
System.out.println("------------------------------------------------------------------");
System.out.println("[Back to the last Question.]");
}
else if (Enter.equalsIgnoreCase("no") || Enter.equalsIgnoreCase("n")) {
System.out.println("[Okay bye.] Exitting...");
WhileStop = false;
}
else {
System.out.println("[Your answer didn't equal.] Exitting...");
WhileStop = false;
}
}
return false;
}
public static boolean CheckInput(String a){ //Return boolean value this method helps minimize code lines
Enter = a.trim();
for (int i = 0; i < 3; i++) {
try {
if (Enter.equalsIgnoreCase(ChoicesAndQuestions[1][10][i])) {
return true;
}
else if (CheckAlphabetic(Enter) == true && (!(Enter.equalsIgnoreCase("A") || Enter.equalsIgnoreCase("B") || Enter.equalsIgnoreCase("C") || Enter.equals("")))) {
throw new ExceedLetterException(); //throw a exception to catch the error of the invalid that didn't include in ABC Choices.
}
else if (IsNumber(Enter) == 1 || CheckAlphabetic(Enter) == false) {
throw new NumberOrSpecialCharacterException(); //throw a exception to catch if the user input a numbers or special characters.
}
else if (Enter.isEmpty()) {
throw new NullPointerException(); //throw a exception to catch if the user input a blank or space character.
}
}
catch (NullPointerException e) {
if (Block == 1) {
Block = 0;
System.out.println("[The Answer cannot be blank.]"); //Give error message from NullPointerException
}
}
catch (NumberOrSpecialCharacterException e) { //Give error message from NumberOrSpecialCharacterException a custom exception
if (Block == 1) {
Block = 0;
System.out.println(e.getMessage());
}
}
Last edited: