0

This is what I have:

import java.util.*;
import java.text.*;

public class Lab4 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System. in );
        Scanner keyboard = new Scanner(System. in );
        String input;
        int students;
        int correctAnswers = 0;

        char[] answerKey = {
            'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'A'
        };
        char[] userAnswers = new char[answerKey.length];

        DecimalFormat df = new DecimalFormat("#0.0");

        System.out.print("how many students are in your class?");
        input = s.nextLine();
        students = Integer.parseInt(input);

        String[] name = new String[students];

        for (int j = 0; j < students; ++j) {
            System.out.print("Enter name of student " + (j + 1) + ": ");
            name[j] = s.nextLine();

            System.out.print("Enter quiz score answers :");
            for (int k = 0; k < answerKey.length - 1; ++k) {
                userAnswers[k] = s.next().charAt(0);
            }

            for (int i = 0; i < userAnswers.length; i++) {

                if (userAnswers[i] == answerKey[i]) {
                    correctAnswers++;
                }

            }

            System.out.println((df.format((correctAnswers / answerKey.length) * 100)) + "%");

        }

    }

}

But every time I enter in the 12 answers ( correct ones even) it just goes to the next line and doesn't print anything else, idk what is wrong with it, I'm thinking maybe the userAnswers aren't getting assigned correctly?

Anyways any help would be appreciated. thanks

Karthik T
  • 31,456
  • 5
  • 68
  • 87
Joseph Mindrup
  • 65
  • 1
  • 3
  • 10
  • What do you mean by "next line" and "anything else"? Does the `println` fail to print? – Karthik T Feb 27 '13 at 02:04
  • You need to work out what your code is actually doing, and where it is executing. Add some debug lines that print out where execution is, particularly in loops, and print out some basic data (such as userAnswers[k]) so you can see what is actually happening. You will quickly see why the code is failing to do what you expect. – jarmod Feb 27 '13 at 02:08
  • like after i type in the characters to be graded against the answer key, the cursor goes to the next line and yeah the next println doesn't print, i think maybe the program terminates its self? i really don't know. – Joseph Mindrup Feb 27 '13 at 02:09
  • it seems to only be assigning the first letter i type in? is the for loop not correct that it doesn't put them all in? – Joseph Mindrup Feb 27 '13 at 02:13
  • See [this article on integer division][1]. [1]: http://stackoverflow.com/questions/3779604/noob-why-divide-always-produces-0-0-float-integer-problem – jarmod Feb 27 '13 at 02:15
  • so my correctAnswers is going to need to be a double? – Joseph Mindrup Feb 27 '13 at 02:19

3 Answers3

0

Couple of things:

  1. This line for (int k = 0; k < answerKey.length -1; ++k) { should be changed to for (int k = 0; k < answerKey.length; ++k) { otherwise you will be missing one answer from the user

  2. You are using two integers when you do this correctAnswers/answerKey.length, so the result will be truncated to 0. You should use double or float as the type for correctAnswersand then use that to do the division to get the decimal values.

Bizmarck
  • 2,663
  • 2
  • 33
  • 48
0

First, the cursor goes to the next line and yeah the next println doesn't print (quoted from your comment). It's happen because you only print "Enter quiz score answers : " once outside the loop.

Try to change this line:

  System.out.print("Enter quiz score answers :");
  for (int k = 0; k < answerKey.length - 1; ++k) {
       userAnswers[k] = s.next().charAt(0);
  }

to :

  for (int k = 0; k < answerKey.length; ++k) {
          System.out.print("Enter quiz score answers : ");
          userAnswers[k] = s.next().charAt(0);                  
  }

Second, your have problem with this calculation :

System.out.println((df.format((correctAnswers / answerKey.length) * 100)) + "%");

where correctAnswers and answerKey.length is int, so the the result of correctAnswers / answerKey.length is int.

What happen if the correctAnswers = 4 ? It will calculate 4/12 first. Since both is an int, it will result to 0.

Try to change that line to this:

 System.out.println((df.format((((float)(correctAnswers) / answerKey.length)) * 100)) + "%");
Iswanto San
  • 18,263
  • 13
  • 58
  • 79
  • This is so helpful thank you, my only problem is that my teacher wants all 12 characters to be entered on that one line, that's why i had it before the fore loop, is there no way to do that this way? – Joseph Mindrup Feb 27 '13 at 02:26
  • System.out.print("Enter quiz score answers :"); String line = s.nextLine(); for (int k = 0; k < answerKey.length; k++) { userAnswers[k] = line.charAt(k); } this is what i changed the code to and it seemed to work – Joseph Mindrup Feb 27 '13 at 02:45
0

Scanner.next() uses delimiters to tokenize input strings. If you don't set a delimiter using the Scanner.useDelimiter, the default is whitespace. Therefore, you can either separate each answer with whitespace as you enter them, or you can set the delimiter to the empty string so that each character is taken separately. I suspect that might be what the Scanner keyboard = new Scanner(System. in ); line is for - currently it is not used, but could be used with an empty delimiter to parse the answer string without spaces.

To see what I'm talking about, try entering each answer separated by a space. You can also try entering "ab ab ab..." for each of the twelve answers (i.e. 12 x "ab "), and you should see that your code will read in 12 "a"s as the answer.

ajmccluskey
  • 505
  • 4
  • 10