Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: index help

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default index help

    im trying to do a read/write int property called currentQuestionIndex. This property is the index of the question currently selected, and the value of the property is 0 when a DrivingTest object is first created. I dont understand how to do this. im not sure how to get the index of the current question. i was trying to use an iterator but i dont think i was doin it right.




    my question class that hold all my data
     
    package cs320Labs.servlet;
     
    public class Question {
    	String description;
    	String answerA;
    	String answerB;
    	String answerC;
    	int correctAnswer;
    	int answer;
    	boolean answerCorrect;
    public Question(){
     
     
    }
    	public Question(String description, String answerA, String answerB,
    			String answerC, int correctAnswer) {
    		this.description = description;
    		this.answerA = answerA;
    		this.answerB = answerB;
    		this.answerC = answerC;
    		this.correctAnswer = correctAnswer;
     
    	}
     
    	public String getDescription() {
    		return description;
    	}
     
    	public void setDescription(String description) {
    		this.description = description;
    	}
     
    	public String getAnswerA() {
    		return answerA;
    	}
     
    	public void setAnswerA(String answerA) {
    		this.answerA = answerA;
    	}
     
    	public String getAnswerB() {
    		return answerB;
    	}
     
    	public void setAnswerB(String answerB) {
    		this.answerB = answerB;
    	}
     
    	public String getAnswerC() {
    		return answerC;
    	}
     
    	public void setAnswerC(String answerC) {
    		this.answerC = answerC;
    	}
     
    	public int getCorrectAnswer() {
    		return correctAnswer;
    	}
     
    	public void setCorrectAnswer(int correctAnswer) {
    		this.correctAnswer = correctAnswer;
    	}
     
    	public int getAnswer() {
    		return answer;
    	}
     
    	public void setAnswer(int answer) {
    		this.answer = answer;
    	}
     
    	public boolean isAnswerCorrect() {
     
    		if (this.answer == correctAnswer) {
    			return answerCorrect == true;
     
    		} else {
    			return answerCorrect == false;
    		}
    	}
     
    	public void setAnswerCorrect(boolean answerCorrect) {
    		this.answerCorrect = answerCorrect;
    	}
     
    	public static void main(String[] args) {
     
    	}
     
    }

    my driving test class
     
    public class DrivingTest {
    	static List<Question> driveTestQ = new ArrayList<Question>();
    	DrivingTest(){
     
    		List<Question> driveTestQ = new ArrayList<Question>();
     
    		File file = new File("C:/Users/Anthony/Desktop/DrivingTest.txt");
     
    		try {
     
    			Scanner scanner = new Scanner(file);
     
    			while (scanner.hasNextLine()) {
    				//store first line into qestions
    				String question = scanner.nextLine().trim();
    				if (question.length() == 0)
    					continue;
     
    				String[] answers = new String[3];
                 // store answers into answers array
    				answers[0] = scanner.nextLine();
    				answers[1] = scanner.nextLine();
    				answers[2] = scanner.nextLine();
    				// stores correct answer
    				int correct_answer = Integer.parseInt(scanner.nextLine());
                    //adds into array list, mus tmatch ur Quetions class
    				driveTestQ.add(new Question(question, answers[0], answers[1],
    						answers[2], correct_answer));
     
     
     
    			}
    			scanner.close();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
     
    	}
     
    	public static int getCurrentQuestionIndex(){
    		ListIterator listIterator = driveTestQ.listIterator();
    		return listIterator.nextIndex();
     
     
    	}
    	public static int setCurrentQuestionIndex(int index){
     
     
    		return index;
     
     
    	}
     
    	public static int getCurrentQuestion(){
    		return 0;
     
     
    	}
    	public static boolean islastQuestion (){
     
    		boolean current_question = false;
    		boolean last_question = false;
    		if(current_question==last_question){
    			return false;
     
    		}
    		return true;
     
    	}
     
    	public static int getScore(){
     
    		int number_of_correct_answers = 0;
    		int number_of_questions = 0;
    		int score;
    		score =(int) ((double)Math.round(100 * number_of_correct_answers / number_of_questions));
    		return score;
     
     
    	}
     
    	public static void main(String[] args) {
     
    		DrivingTest test = new DrivingTest();
    		System.out.println(test.getCurrentQuestionIndex());
    		System.out.println(test.setCurrentQuestionIndex(2));
     
     
    //		 while( true )
    //		    {
    //		        // display the current question
    //		        Question question = DrivingTest.getCurrentQuestion();
    //		        System.out.println( question.getDescription() );
    //		        System.out.println( "\t" + question.getAnswerA() );
    //		        System.out.println( "\t" + question.getAnswerB() );
    //		        System.out.println( "\t" + question.getAnswerC() + "\n" );
    //		
    //		        // set the answer to the current question to 1
    //		        DrivingTest.getCurrentQuestion().setAnswer( 1 );
    //		
    //		        // if this is the last question, we are done.
    //		        if( DrivingTest.islastQuestion() ) break;
    //		
    //		        // it is not the last question, so increment CurrentQuestionIndex
    //		        int currentQuestionIndex = DrivingTest.getCurrentQuestionIndex();
    //		       DrivingTest.setCurrentQuestionIndex( currentQuestionIndex + 1 );
    //		    }
    //		
    //		    // display the test score
    //		    System.out.println( "Your test score is: " + DrivingTest.getScore() );
    //		}
    //		
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: index help

    how to get the index of the current question
    Are you asking about the contents of a variable in the program
    or about reading a value from a file?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: index help

    Quote Originally Posted by Norm View Post
    Are you asking about the contents of a variable in the program
    or about reading a value from a file?
    hah im confused in that as well . but i guess it would be the contents of a variable . my program reads in a text file , and in that text file the first line is a question. so im supposed use this method to grab whatever the index of the current question is.

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: index help

    Okay lets take an example.

    The text file contains the below:

    What is your name
    Brad
    William
    Simon
    1

    Where are you from
    USA
    UK
    Germany
    1


    So, you want to set the index of the question in "currentQuestionIndex" variable.

    Index for "What is your name" is 1
    Index for "Where are you from" is 2

    Correct me if I am wrong!!
    Thanks and regards,
    Sambit Swain

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: index help

    Are you numbering the questions starting at 0 or starting at 1? (0 is standard index for the first item in a list)
    By what value will the index be changed? For example if the first question's index is 1 what will be the index for the next question?

    When you have decided what value the first item in the list will have then use that value to initialize the current question index variable.
    Then when the code moves to the next question, increment the value of the variable.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: index help

    Thanks Norm, your solution is more easy. In Question.java use a new property "index" and populate all the time when you read a question.
    Thanks and regards,
    Sambit Swain

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: index help

    i gotcha guys , thanks. ill try it as soon as i get off of work lol. thanks for the help! the first value should be 0.

Similar Threads

  1. Replies: 3
    Last Post: August 26th, 2013, 04:41 PM
  2. help with index in array
    By lanya1 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 6th, 2013, 08:58 PM
  3. Need help with index of coincedence
    By tripline in forum Java Theory & Questions
    Replies: 4
    Last Post: October 30th, 2011, 11:29 AM
  4. index of array
    By nasi in forum Java Theory & Questions
    Replies: 1
    Last Post: April 12th, 2010, 02:39 AM
  5. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 12:19 PM