Your for loop overruns the number of the quiz proposed questions. I've fixed that (and other bugs, adding remarks on modified lines of code). Try
(Please note, using
Java
facilities, there are far better ways to write such a code).
import java.io.*;
import java.util.*;
public class GameGuess{
int x;
int ctr;
public static void main(String [] args){
new GameGuess();
}
public GameGuess(){
GameGuess1();
}
public void GameGuess1(){
Scanner con = new Scanner(System.in);
String[] z= new String[5];
String[] a= new String[5];
String[] b= new String[5];
String[] c= new String[5];
String[] q= new String[5];
String[] answer= new String[5];
String[] validLetters= new String[3];
int ctr = 0;
validLetters[0] = "a";
validLetters[1] = "b";
validLetters[2] = "c";
q[0] = "capital city of philippines?";
q[1] = "1 + 2?";
q[2] = "9+1?";
q[3] = "5+5?";
q[4] = "3+5?";
a[0] = "manila";
b[0] = "davao";
c[0] = "gensan";
answer[0] = "a";
a[1] = "3";
b[1] = "6";
c[1] = "8";
answer[1] = "a";
a[2] = "11";
b[2] = "3";
c[2] = "10";
answer[2] = "c";
a[3] = "20";
b[3] = "10";
c[3] = "6";
answer[3] = "b";
a[4] = "3";
b[4] = "8";
c[4] = "9";
answer[4] = "b";
System.out.println("Quiz Bee");
for (int x=0; x<5; x++){
try{
System.out.println(x+1 + " . " + q[x]);
System.out.println("A." + a[x]);
System.out.println("B." + b[x]);
System.out.println("C." + c[x]);
System.out.println("Enter your answer:");
String myans =con.next();
System.out.println(" ");
if(myans.equalsIgnoreCase("a") || myans.equalsIgnoreCase("b") || myans.equalsIgnoreCase("c")){
if (myans.equalsIgnoreCase(answer[x])){
ctr++;
}
}else if(myans.equalsIgnoreCase(" ") || myans.equalsIgnoreCase(" ")){
throw new BlankAnsException();
}
else if (myans.equalsIgnoreCase("0") || myans.equalsIgnoreCase("1") || myans.equalsIgnoreCase("2") || myans.equalsIgnoreCase(".")){
throw new Exception("number or Special characters are not allowed");
}
else{
throw new InvalidLetterException();
}
}catch(InvalidLetterException ide){
System.out.println(ide.getMessage());
--x;
}
catch(BlankAnsException dae){
System.out.println(dae.getMessage());
--x;
}
catch (Exception e){
System.out.println(e.getMessage());
--x;
}
}
System.out.println("Your score is:" + ctr + "/5.");
}public class InvalidLetterException extends Exception{
public InvalidLetterException(){
super("An invalid letter.. Enter only A, B and C only.\n");
}
}