I am trying to create a program that spell checks what the user has typed in. They have to type the word 'type' in 10 times correctly and it will display a message saying how long it took.
My problem is that the end never comes, it continually ask you to type in 'type'. I believe this is because of the 'while (attempts < required)'. I think I need to make required = correct attempt. if attempt = type, then it will produce 1 correct value. Any help is appreciated. also apologies, I don't know how to structure the question
public static void printHeading(String heading) {
System.out.println(heading.toUpperCase());
for (int i = 0; i < heading.length(); i++)
System.out.print("=");
System.out.println();
System.out.println();
}
public static int runTutorial(Scanner in, String word, int required) {
Scanner sc = new Scanner(System.in);
String attempt = word;
int correct = 0;
int attempts = 0;
while (attempts<required)
{
System.out.print("Enter '" + word + "': ");
attempt = sc.nextLine();
if(attempt.equals("type")) {
System.out.println("Correct");
}
else {
System.out.println("Try again");
}
}
return attempts;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final int TIMES = 10; //required number of correct repetitions
final String WORD = "type"; //the word to type
long startTime, endTime; //start and end time of typing test
double seconds; //elapsed time in seconds
int attempts;
printHeading("Typing Tutor");
System.out.println("You need to type a word " + TIMES +
" times correctly, as quickly as you can");
System.out.println("Your word today will be '" + WORD + "' (do not enter the quotes)");
System.out.println();
System.out.println("Type anything and press enter to begin");
//The test
System.out.println("Press enter to start the test");
sc.nextLine();
startTime = System.currentTimeMillis();
attempts = runTutorial(sc, WORD, TIMES);
endTime = System.currentTimeMillis();
//Test report
seconds = (double)(endTime - startTime) / 1000;
System.out.println("You took " + seconds + " seconds and " + attempts +
" attempts to correctly type '" + WORD + "' " + TIMES + " times");
System.out.println();
printHeading("Come back for more pracice soon");
}
}
attemptsin yourwhileloop, its value is always0.