1

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");
    }

}
3
  • 2
    You forgot to increment attempts in your while loop, its value is always 0 . Commented Oct 4, 2019 at 10:26
  • 1
    what has this got to do with declaring a variable? Commented Oct 4, 2019 at 10:27
  • @JohnSmith your question is unclear. you want us to help you, I would recommend first learning the basic terminology. there is declaring a variable, instantiating a variable, setting the value of a variable. No, they're not the same. A variable has to be declared before you can even dream of using it, that much is true. But your question in the title is about declaring a variable, your explanation is about something different alltogether Commented Oct 4, 2019 at 12:20

1 Answer 1

0

Under System.out.println("Correct");, you need to increment the attempts variable, otherwise it will remain at 0.

if(attempt.equals("type")) {

    System.out.println("Correct");
    attempts++; // <-- here
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.