Results 1 to 3 of 3
Thread: help returning array
- 03-02-2013, 10:23 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 7
- Rep Power
- 0
help returning array
I have a drivers license test program where in the main method the user inputs the answers then in a separate class there needs to be a method to see if the user passed a method to tell how many were wrong and right and a method put the numbers of the answers which were wrong into an array then the main method needs to print out the results of each of those methods. my issues is with the array of the problem numbers that were wrong . I believe I correctly created it but I cant get the main method to print it out.Thanks
Java Code:public class DriverExam { static String[] correctAnswers={"b","d","a","a","c","a","b","a","c","d","b","c","d","a","d","c","c","b","d","a"}; static String[] userAnswers=new String[20]; //constructer public DriverExam(String[] user) { userAnswers=user; } //method to see if you passed public static boolean passed() { boolean pass=false; int correctCount=0; int incorrectCount=0; for(int i=0;i<userAnswers.length;i++) { if(userAnswers[i].equals(correctAnswers[i])) { correctCount++; } else { incorrectCount++; } } if(correctCount>14) {pass=true;} return pass; } //method to find number correct public static int totalCorrect() { boolean pass=false; int correctCount=0; int incorrectCount=0; for(int i=0;i<userAnswers.length;i++) { if(userAnswers[i].equals(correctAnswers[i])) { correctCount++; } else { incorrectCount++; } } return correctCount; } //method to tell how many were not correct public static int totalIncorrect() { boolean pass=false; int correctCount=0; int incorrectCount=0; for(int i=0;i<userAnswers.length;i++) { if(userAnswers[i].equals(correctAnswers[i])) { correctCount++; } else { incorrectCount++; } } return incorrectCount; } public static int[] questionsMissed() { boolean pass=false; int correctCount=0; int incorrectCount=0; for(int i=0;i<userAnswers.length;i++) { if(userAnswers[i].equals(correctAnswers[i])) { correctCount++; } else { incorrectCount++; } } int[] questionWrong=new int[incorrectCount]; for(int i=0;i<questionWrong.length;i++) { if(!userAnswers[i].equals(correctAnswers[i])) {i=questionWrong[i]; } } return questionWrong; } }
Java Code:import java.util.Scanner; public class DriverExamDemo { public static void main(String[] args) { String[] userAnswers=new String[20]; String answer; System.out.println("please enter the testee's answers as the correspond with the question number"); for(int i=0;i<userAnswers.length;i++) { Scanner input=new Scanner(System.in); System.out.println("Q."+(i+1)); answer=input.next(); userAnswers[i]=answer; } // constructor to send the users answers DriverExam exam1 = new DriverExam(userAnswers); //Check passed boolean to see if the user passed or failed if(DriverExam.passed()) System.out.println("you passed your driving exam"); else System.out.println("you failed your driving exam"); //check how many answers were correct and incorrect System.out.println("you answered "+DriverExam.totalCorrect()+" questions correctly"); System.out.println("you answered "+DriverExam.totalIncorrect()+" questions incorrectly"); int[] questionsWrong=DriverExam.questionsMissed(); for (int i=0;i<questionsWrong.length;i++) { System.out.println("you got question " +questionsWrong[i]+"incorrect"); } } }
- 03-02-2013, 11:01 AM #2
Member
- Join Date
- Nov 2012
- Location
- India
- Posts
- 70
- Rep Power
- 0
Re: help returning array
Try this:
i think you except to print this
you got question 0incorrect
you got question 1incorrect
you got question 2incorrect
you got question 3incorrect
So you made one mistake on questionsMissed mehod on DriverExam class that's like you declare the array inside the loop. each time it's created.
So You try this...
Java Code:static int[] questionWrong ;// declare globally public static int[] questionsMissed() { boolean pass=false; int correctCount=0; int incorrectCount=0; for(int i=0;i<userAnswers.length;i++) { if(userAnswers[i].equals(correctAnswers[i])) { correctCount++; } else { ++incorrectCount; } } questionWrong=new int[incorrectCount]; // assign value here /* for(int i=0;i<questionWrong.length;i++) { if(!userAnswers[i].equals(correctAnswers[i])) {i=questionWrong[i]; } }*/ return questionWrong; }Regards
Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475
- 03-02-2013, 05:48 PM #3
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
Re: help returning array
The code in #2 serves no purpose but to return an empty array.
The current code actually causes an infinate loop where i is not being incremented because of the code on line 106. After initialization the int array 'questionWrong' is populated with zeros. The code on line 106 assigns the value of 'qustionWrong[i]' to 'i', in this case 0. Because 'i' is reset to 0 during each iteration it creates the infinate loop.
There are more efficient ways to acheive the results you want, but based on the code...
You've defined an array, 'questionWrong', to be the length of the number of incorrect answers. As you loop through the answers you want to assign the question number of each incorrect answer to the array.
For this create another variable to hold the position of the next available element of 'questionWrong'. When looping through the answers assign the question number to the element and increase the value of the position variable by one.
Let me know if you require any further assistance.
Regards.
Similar Threads
-
HELP. Getting an error returning a 2d array
By Orangepillow in forum New To JavaReplies: 22Last Post: 02-12-2012, 07:11 PM -
Returning Array Row
By Zocheyado in forum New To JavaReplies: 2Last Post: 04-15-2011, 02:23 AM -
returning double array
By Billaguana in forum New To JavaReplies: 2Last Post: 01-16-2011, 03:59 AM -
returning array
By aizen92 in forum New To JavaReplies: 4Last Post: 01-08-2011, 03:10 PM -
Returning An Array
By elektronika in forum New To JavaReplies: 2Last Post: 12-07-2009, 03:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks