I've been making a quiz in java, and everything seems fine, but when i tested it, it gave me a long list of errors. This is the whole code:
Main class:
Methods class:
Questions class:
But when i run it i get a really long list of errors, at the top it says
Exception in thread "main" java.lang.StackOverflowError
at java.lang.StringBuffer.setLength(Unknown Source)
at java.text.DecimalFormat.expandAffix(Unknown Source)
at java.text.DecimalFormat.expandAffixes(Unknown Source)
at java.text.DecimalFormat.applyPattern(Unknown Source)
at java.text.DecimalFormat.<init>(Unknown Source)
at java.text.NumberFormat.getInstance(Unknown Source)
at java.text.NumberFormat.getNumberInstance(Unknown Source)
at java.util.Scanner.useLocale(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
And then it keeps saying
at Questions.<init>(Questions.java:4)
at Methods.<init>(Methods.java:9)
over and over and over again.
The problem is that i have no idea why I'm getting all these errors! I'm using Eclipse Juno.
Main class:
public class Main {
public static void main(String args[]){
Methods me = new Methods();
me.menu();
}
}
Methods class:
import java.util.Scanner;
import java.util.Random;
public class Methods {
//Attributes
//-------------------------------------------------------------------
private int correctq = 0;
private int incorrectq = 0;
private int totalq = 0;
private Questions qe = new Questions();
private Scanner sc = new Scanner(System.in);
private Random ra = new Random();
//Methods
//-------------------------------------------------------------------
public void menu(){
cls();
System.out.println("This is a quiz that will generate a\nrandom question for you to answer!");
System.out.println();
System.out.println("Do you want to continue? Y/N");
char YN = sc.nextLine().charAt(0);
if(YN == 'Y' || YN == 'y'){
randomq();
}
if(YN == 'N' || YN == 'n'){
System.exit(0);
}
}
public void correct(){
cls();
correctq++;
totalq++;
System.out.println("Correct!\nDo you want another question? Y/N");
char YN = sc.nextLine().charAt(0);
if(YN == 'Y' || YN == 'y'){
randomq();
}
if(YN == 'N' || YN == 'n'){
end();
}
}
public void incorrect(){
cls();
incorrectq++;
totalq++;
System.out.println("Incorrect!\nDo you want another question? Y/N");
char YN = sc.nextLine().charAt(0);
if(YN == 'Y' || YN == 'y'){
randomq();
}
if(YN == 'N' || YN == 'n'){
end();
}
}
public void end(){
cls();
System.out.println("Game finished!\nYou got " + correctq + "/" + totalq + " correct questions");
System.out.println("You got " + incorrectq + "/" + totalq + " incorrect questions");
System.out.println("Total questions answered: " + totalq);
System.out.println("Do you want to go back to menu? Y/N");
char YN = sc.nextLine().charAt(0);
if(YN == 'Y' || YN == 'y'){
menu();
}
if(YN == 'N' || YN == 'n'){
System.exit(0);
}
}
public void randomq(){
int rand = 1+ra.nextInt(5);
if(rand == 1){
qe.q1();
}
if(rand == 2){
qe.q2();
}
if(rand == 3){
qe.q3();
}
if(rand == 4){
qe.q4();
}
if(rand == 5){
qe.q5();
}
}
public void cls(){
for(int counter = 0; counter <= 10; counter++){
System.out.println();
}
}
}
Questions class:
import java.util.Scanner;
public class Questions {
private Scanner sc = new Scanner(System.in);
private Methods me = new Methods();
//I haven't created the questions themselves yet
public void q1(){}
public void q2(){}
public void q3(){}
public void q4(){}
public void q5(){}
}
But when i run it i get a really long list of errors, at the top it says
Exception in thread "main" java.lang.StackOverflowError
at java.lang.StringBuffer.setLength(Unknown Source)
at java.text.DecimalFormat.expandAffix(Unknown Source)
at java.text.DecimalFormat.expandAffixes(Unknown Source)
at java.text.DecimalFormat.applyPattern(Unknown Source)
at java.text.DecimalFormat.<init>(Unknown Source)
at java.text.NumberFormat.getInstance(Unknown Source)
at java.text.NumberFormat.getNumberInstance(Unknown Source)
at java.util.Scanner.useLocale(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
And then it keeps saying
at Questions.<init>(Questions.java:4)
at Methods.<init>(Methods.java:9)
over and over and over again.
The problem is that i have no idea why I'm getting all these errors! I'm using Eclipse Juno.