So, I guess I have a stupid mistake here someplace

I'm sorry, but I'm pulling my hair out here just trying to find out where I went wrong. I had to create a loop (I used while) at the end of this program so that the teacher using it can grade multiple quizzes on the same key. It compiles and runs fine until the very end where it asks if I would like to continue with another quiz. It prints out the message, but doesn't give me a chance to respond and instead automatically ends the program. I even looked up a very similar example in my text book, and as far as I can tell, they did it exactly like I did. Any help would be greatly appreciated. Thank you.

import java.util.Scanner;

public class Grades

{

public static void main (String[] args)

{

int questions;

int counter = 0;

int average;

String another = "y";

Scanner scan = new Scanner(System.in);

System.out.print("Please enter the number of questions on the quiz: ");

questions = scan.nextInt();

int[] key = new int [questions + 1];

int[] answer = new int [questions + 1];

for (int i=1; i<key.length; i++)

{

System.out.print("Please enter the answer to question " + i + ": ");

key = scan.nextInt();

}

while(another.equalsIgnoreCase("y"))

{

for (int i=1; i><key.length; i++)

{

System.out.print("Please enter the answer the student gave for question " + i + ": ");

answer = scan.nextInt();

if(key == answer)

counter++;

}

average = 100 * counter/questions;

System.out.println("The student had a total of " + counter + " correct answers.");

System.out.println("The student's average on the quiz: " + average + "%");

System.out.println();

System.out.print("Grade another quiz (y/n)? ");

another = scan.nextLine();

}

}

}>

[1902 byte] By [kwyjibo348a] at [2007-11-27 1:07:38]
# 1
Apparently I don't know how to post code here...some of it was used to make the code italicized. I don't think that's the important part though, so it shouldn't matter.
kwyjibo348a at 2007-7-11 23:42:54 > top of Java-index,Java Essentials,Java Programming...
# 2
It's because [i] is used as an italics tag. So the program prompts you to grade another quiz, but doesn't wait for input?
CaptainMorgan08a at 2007-7-11 23:42:54 > top of Java-index,Java Essentials,Java Programming...
# 3

Yeah, it makes it all the way down to the final print statement, but I guess the scanner class isn't letting me input a response. I can input information on everything else though.

Let me try posting the code one more time, see if this helps any.

import java.util.Scanner;

public class Grades

{

public static void main (String[] args)

{

int questions;

int counter = 0;

int average;

String another = "y";

Scanner scan = new Scanner(System.in);

System.out.print("Please enter the number of questions on the quiz: ");

questions = scan.nextInt();

int[] key = new int [questions + 1];

int[] answer = new int [questions + 1];

for (int i=1; i<key.length; i++)

{

System.out.print("Please enter the answer to question " + i + ": ");

key[i] = scan.nextInt();

}

while(another.equalsIgnoreCase("y"))

{

for (int i=1; i><key.length; i++)

{

System.out.print("Please enter the answer the student gave for question " + i + ": ");

answer[i] = scan.nextInt();

if(key[i] == answer[i])

counter++;

}

average = 100 * counter/questions;

System.out.println("The student had a total of " + counter + " correct answers.");

System.out.println("The student's average on the quiz: " + average + "%");

System.out.println();

System.out.print("Grade another quiz (y/n)? ");

another = scan.nextLine();

}

}

}

Message was edited by:

kwyjibo348

Message was edited by:

kwyjibo348>

kwyjibo348a at 2007-7-11 23:42:54 > top of Java-index,Java Essentials,Java Programming...
# 4
scan.nextLine() returns the line that was skipped. see the javadocs at http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#nextLine() Instead you use scan.next() I hope it will work fine..
diptaPBa at 2007-7-11 23:42:54 > top of Java-index,Java Essentials,Java Programming...
# 5

When you do answer[i] = scan.nextInt();

and the user enters the number 6 followed by the ENTER key, the string "6\r\n" (Windows) or "6\n" (*nix) will be created. The nextInt() method only reads the number 6 and leaves the line break in the Scanner. So when you do another nextInt() call on the Scanner, you'll receive a type-mismatch error/exception.

I suggest, when reading numbers and String from the console, to read one line at a time and convert the line to an integer like this:answer[i] = Integer.parseInt(scan.nextLine());

Good luck.

prometheuzza at 2007-7-11 23:42:54 > top of Java-index,Java Essentials,Java Programming...