Reputation: 185
I am developing a small application to grade Multiple Choice Questions submitted by the user. Each question has obviously 4 choices. A,B,C,D. Since these answers will be stored in a two dimensional array, I want to ask how can I take input from user for char variable. I have not learnt any method to take input for char arrays on console. i.e I have just worked with nextInt(), nextDouble(), nextLine() etc. These methods are for Strings and Integers not for char. How to take input for char arrays? I am going to post code snippet of taking input so that you people can better understand.
public class MCQChecker{
public static void main(String []args)
{
Scanner input=new Scanner(System.in);
char[][] students=new char[8][10];
for (int i=0;i<8;i++)
{
System.out.println("Please enter the answer of "+students[i+1]);
for(int j=0;j<10;j++)
{
students[i][j]=?;//Im stuck here
}
}
}
}
Upvotes: 1
Views: 18761
Reputation: 79
You can't take input directly in charArray as because there is no nextChar() in Java. You first have to take input in String then fetch character one by one.
import java.util.*;
class CharArray{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
char ch[]=new char[11];
String s = scan.nextLine();
for(int i=0;i<=10;i++)
ch[i]=s.charAt(i); //Input in CharArray
System.out.println("Output of CharArray: ");
for(int i=0;i<=10;i++)
System.out.print(ch[i]); //Output of CharArray
}
}
Upvotes: 0
Reputation: 19185
What you need is more than char
to handle your requirement. Create a question class which will have question and correct answer, user entered answer.
public static class Question {
private Choice correctChoice = Choice.NONE;
private Choice userChoice = Choice.NONE;
private String question = "";
public Question(String questionString, Choice choice) {
this.question = questionString;
this.correctChoice = choice;
}
public void setUserChoice(String str) {
userChoice = Choice.valueOf(str);
}
public boolean isQuestionAnswered() {
return correctChoice == userChoice;
}
public String question() {
return question;
}
}
enum Choice {
A, B, C, D, NONE
}
Now you can create a List of questions and for each question you can check whether it was answered correctly or not.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
List<Question> questions = new ArrayList<Question>();
questions.add(new Question("question1", Choice.A));
questions.add(new Question("question2", Choice.A));
questions.add(new Question("question3", Choice.A));
for (Question q : questions) {
System.out.println("Please enter the answer of " + q.question());
String str = input.next();
q.setUserChoice(str);
System.out.println("You have answered question "
+ (q.isQuestionAnswered() == true ? "Correctly"
: "Incorrectly"));
}
}
Above program now allows you to ask questions and reply to user accordingly. When question is asked if choice entered other than correct answer then question will be marked incorrectly.
In above example if other character is entered than A
then it will tell user that you are incorrect.
Upvotes: 0
Reputation: 67502
Once you get the .next()
value as a String
, check if its .length() == 1
, then use yourString.charAt(0)
.
Upvotes: 3