1

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
            }
        }
    }
}
1
  • FYI don't indent your code in the SO editor. Paste fully formatted code from your IDE. Commented Oct 13, 2012 at 8:07

4 Answers 4

3

Once you get the .next() value as a String, check if its .length() == 1, then use yourString.charAt(0).

Sign up to request clarification or add additional context in comments.

2 Comments

@Hammad Getting character will not get you anywhere. Check My answer. You need far more than that.
@AmitD Your answer is a complete rewrite of the OP's problem and situation. Giving him a full solution teaches him nothing, as all you've done is overwhelm him with concepts of enums and extra classes. His method will work fine for the small scale on which he's executing this project.
3
   students[i][j]=input.next().charAt(0);

Comments

0

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.

Comments

0

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
    }
}

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.