Results 1 to 8 of 8
  1. #1
    theBurgh22 is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default Can't seem to figure how to even get started on writing my code..

    I am in an Intermediate Progamming with Java class and I'm having a lot of trouble on figuring out this assignment. Here is the idea..

    In this assignment I have to implement a simple multiple choice quiz, using good object-oriented programming style. The user will be shown some questions that he/she will answer. After completing all of the questions, the user will find out how many he/she got right and wrong. He/she will also then be shown the correct answers and the average scores for all users, followed by a sorted list of questions.

    The program must read a number of questions from a text file (which I have created) and each question consists of:
    1) The question itself, in sentence on a single line.
    2) An integer, N, indicating how many answers the question has(one line)
    3) The actual N answers, 1 per line
    4) An integer, K indicating which answer is correct(single line)
    5) An integer, T indicating how many times the question was tried(1 line)
    6) An integer, C indicating how many times it was answered correctly(1line)

    Here is my Main Class code...
    Java Code:
    import java.util.*;
    import java.util.Scanner;
    import java.IO.*;
    
    public class Assign {
    	public static void main(String[] args) {
    		Question [] theQs;
    		
    		final int numLines = 46;
    		int index = 0;
    		File qFile = new File("questions.txt");
    		Scanner scan = new Scanner(qFile);
    		theQs = new Question[numLines];
    		
    		while (scan.hasNext() && index < theQs.length){
    			theQs[index] = new Question();
    			theQs[index].getData(qFile);
    			index++;
    		}
    Here is my Question Class...
    Java Code:
    import java.IO.*;
    import java.util.*;
    import java.util.Scanner;
    
    public class Question {
    	
    	private String question;
    	private String [] answers;
    	private int correctA;
    	private int numTries;
    	private int numCorrect;
    	
    	public Question (String Q, int corA, int numT, int numC, String[] ans) {
    		
    	}
    and here is my textfile with the Questions
    Java Code:
    5
    Who is the captain of the Pittsburgh Penguins?
    4
    Evgeni Malkin
    Jordan Stall
    Sidney Crosby
    Pascal Dupuis
    2
    0
    0
    How many Stanley Cups have the Pittsburgh Penguins won?
    5
    One
    Two
    Three
    Four
    Five
    2
    0
    0
    Who is the quarterback of the Pittsburgh Steelers?
    3
    Ben Roethlisburger
    Hines Ward
    James Harrison
    0
    0
    0
    How many Super Bowls have the Pittsburgh Steelers won?
    6
    One
    Two
    Three
    Four
    Five
    Six
    5
    0
    0
    How many national championships has PITT football won?
    2
    Seven
    Nine
    1
    0
    0

  2. #2
    theBurgh22 is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default

    I have to write a class called Question, which i have posted, to represent the questions. it must contain a String for the question itself, an array of strings for the possible answers, an int to indicate the correct answer and two ints to store the # of times tried and # of times correct. I also need to use arrays in multiple places: in my Question class- array of Strings for possible answers.. and in my Main class- array of Question objects to store the questions.

    Am I anywhere near where i need to be? I'm not sure on how to read in the questions from the text file into an array, and then display them with the answers to the user

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Ergh, ignore the type in italics. I was replying before you posted that 2nd post.
    So what's the question? If you're looking for someone to do it for ya, you're in the wrong place...


    If you want a bit of a hint as to a direction to start, you're already on the right track. I do have one problem with your input file though--there's a 5 at the top. Why?

    As for the actual code, you'll want a loop that assumes you start with a sentence (hence my issue above), then reads one line for the sentence, one line for an integer (call it, say, numAnswers), then a loop that loops numAnswers times and reads a line each, then puts that in an array... then you want your main loop to continue 3 more times, each saving the integer on that line. Then you'll have all your variables for the Question() ctor.

    Does that help?

  4. #4
    theBurgh22 is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default

    The 5 at the top of the file is the number of questions that it contains. It is a requirement. Where would I put the loop that starts with a sentence, in my main class or the question class?

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Main class. The question class just contains data about each question.

  6. #6
    theBurgh22 is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default

    Okay thank you very much, that helps alot. I'm going to work on this for a little longer tonight and tomorrow during the day, and if i still have problems or any questions I will post my updated Source codes for both classes and ask my questions tomorrow. Again thank you for your help, I greatly appreciate it.

  7. #7
    theBurgh22 is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default

    Okay here is my updated main Class..

    Java Code:
    import java.util.*;
    import java.util.Scanner;
    import java.io.*;
    
    public class Assign3 {
    	public static void main(String[] args) throws IOException {
    		Question [] theQs;
    		
    		final int numLines = 46;
    		int index = 0;
    		int i = 0;
    		File qFile = new File("questions.txt");
    		Scanner scan = new Scanner(qFile);
    		
    		int numQs = scan.nextInt();
    		theQs = new Question[numQs];
    		
    		while (scan.hasNext() && index < numQs){
    			String quest = scan.next();
    			System.out.println(quest);
    			int numAnswers = scan.nextInt();
    			while (scan.hasNext() && i < numAnswers){
    				String answer = scan.nextLine();
    				i++;
    			}
    		}
    	
    	}
    }
    and here is my Question class...
    Java Code:
    import java.io.*;
    import java.util.*;
    import java.util.Scanner;
    
    public class Question { 
    	
    	private String question;
    	private String [] answers;
    	private int correctA;
    	private int numTries;
    	private int numCorrect;
    	
    	public Question (String Q, int size) {
    		question = Q;
    		answers = new String[size];
    	}
    	
    	public void getData() throws IOException {
    		File qFile = new File("questions.txt");
    		Scanner scan = new Scanner(qFile);
    		
    	}
    }
    The questions.txt file stayed the same.

    When I run my program, it reads only the first word of the first question ("Who") and prints that out. Then I get an error saying: "Exception in thread 'main' java.util.InputMismatchException. Any idea why I am getting this error and why the whole first question won't print out?

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Because you are using next(), which only reads the next **token**. You will want nextLine().

Similar Threads

  1. Getting Started Help (:
    By Jcbconway in forum AWT / Swing
    Replies: 3
    Last Post: 10-10-2010, 07:52 AM
  2. Is this the right way of writing html code in java??
    By raju.i in forum Advanced Java
    Replies: 1
    Last Post: 05-18-2010, 08:05 AM
  3. Replies: 3
    Last Post: 01-11-2010, 06:48 AM
  4. Getting started
    By hornsby in forum NetBeans
    Replies: 1
    Last Post: 03-12-2009, 07:47 AM
  5. How do I get started?
    By programmer-in-training in forum New To Java
    Replies: 4
    Last Post: 01-11-2008, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •