user3516302
user3516302

Reputation: 81

Java Scanner issues, Notecard class

I am trying to make a program that is basically virtual notecards. Each notecard has a string for a question and an answer as well as a count for now many times it has been asked. I am using a scanner in many instances and I think i am using it incorrectly, and am not quite sure why. The program will let me answer the first 2 questions, tell me they are incorrect no matter what, and skip letting me answer the last one. Here is the notecard class:

public class Notecard {

    public String ans;
    public String q;
    public int count;

    public Notecard(String q, String ans) {
        this.q = q;
        this.ans = ans;
        this.count = 0;
    }

    public Boolean answer(String effort) {
        if (this.q.toUpperCase().equals(effort.toUpperCase())) {
            System.out.println("Correct!");
            return true;
        } else {
            System.out.println("Incorrect! Correct answer:" + this.ans);
            count++;
            return false;
        }
    }

    public void clearCount() {
        this.count = 0;
    }

    public String getQ() {
        return this.q;
    }
}

and here is my other file:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;

public class CreateNotecard {

    int trys;

    public static void main(String[] args) {
        System.out.println("Get ready to be quizzed \n\n");
        ArrayList<Notecard> notecards = makeCards();
        quiz(notecards);
    }

    static ArrayList<Notecard> makeCards() {
        ArrayList<Notecard> notecards = new ArrayList<Notecard>();
        try {
            BufferedReader in = new BufferedReader(new FileReader(
                    "notecards.txt"));
            String str;
            str = in.readLine();
            while ((str = in.readLine()) != null) {
                String[] argg = str.split(",");
                notecards.add(new Notecard(argg[0], argg[1]));
            }
            in.close();
        } catch (IOException e) {
            System.out.println("File Read Error");
        }
        return notecards;
    }

    static void quiz(ArrayList<Notecard> notecards) {
        ArrayList<Notecard> backupList = notecards;
        Scanner sc = new Scanner(System.in);
        long seed = System.nanoTime();
        Collections.shuffle(notecards, new Random(seed));
        int total = notecards.size();
        int correct = 0;
        for (Notecard x : notecards) {
            System.out.println(x.getQ());
            String effort = sc.next();
            Boolean nailedIt = x.answer(effort);
            if (nailedIt) {

                correct++;
            }
        }
        System.out.println("Total Notecards: " + total + "\nTotal Correct: "
                + correct);
        System.out.println("Accuracy: " + (correct / total));
        System.out.println("Do you want to repeat? Put \"y\" or \"n\"");
        String choice1 = sc.nextLine();
        if (choice1.toUpperCase().equals("Y")) {
            System.out.println("Use only cards missed or all? Type \"missed\" or \"all\"");
            String choice2 = sc.nextLine();
            if (choice2.toUpperCase().equals("MISSED")) {
                quiz(notecards);
            } else {
                quiz(backupList);
            }
        } else {
            return;
        }

    }

}

I have a text file which I am using for this program, it contains

19-9,10
square root of 4,2
capitol of Missouri,Jefferson City
Blastoise's 1st evolution,squirtle

and my output is

Get ready to be quizzed 


square root of 4
2
Incorrect! Correct answer:2
capitol of Missouri
Jefferson City
Incorrect! Correct answer:Jefferson City
Blastoise's 1st evolution
Incorrect! Correct answer:squirtle
Total Notecards: 3
Total Correct: 0
Accuracy: 0
Do you want to repeat? Put "y" or "n"

Upvotes: 0

Views: 51

Answers (2)

Jim Garrison
Jim Garrison

Reputation: 86774

You are comparing the wrong things:

public Boolean answer(String effort) {
    if (this.q.toUpperCase().equals(effort.toUpperCase())) {

Should be

    if (this.ans.toUpperCase().equals(effort.toUpperCase())) {

Upvotes: 2

Hypino
Hypino

Reputation: 1248

The problem is that the Scanner class is looking for a delimiter to create tokens with, which is by default whitespace. Since you enter "2", the Scanner.next() finds no delimiters, so no token.

For example, if you enter "Jefferson City", the Scanner found one delimiter, so two tokens. sc.next in that case would be "Jefferson" only (no "City", that's the next token).

Solution? Read the line from stdin and using sc.nextLine()

Upvotes: 1

Related Questions