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 3 of 3

Thread: problem returning array

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem 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

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





    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");
     
     
     
    }
    }
    }


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: problem returning array

    The reason isin the questionMissed-method
            for (int i = 0; i < questionWrong.length; i++) {
                if (!userAnswers[i].equals(correctAnswers[i])) {
                    i = questionWrong[i];
                }
            }

    the questionWrong array is initilized with 0 in every position. You set the iterator var every time to zero, so your loop never ends,if there is a wrong question, so the procedure doesnt terminate.

    Change to

    int k=0;      
      for (int i = 0; i < userAnswers.length; i++) {
                if (!userAnswers[i].equals(correctAnswers[i])) {
                    questionWrong[k++]=i;
                }
            }

    Also you cannot iterate over the questionWrong array, because it's smaller -hopefully for the user-then the question and answer array.

    BTW: You mix a little bit the concepts of a static c-like library and the object orientated philosophy of java(setting a static var in a constuctor)

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: problem 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...

     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
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

Similar Threads

  1. [SOLVED] Returning an array
    By maple1100 in forum What's Wrong With My Code?
    Replies: 21
    Last Post: January 21st, 2013, 02:38 PM
  2. Returning An Array
    By LoganC in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 28th, 2012, 03:57 PM
  3. [SOLVED] Issue when returning an array
    By Broxxar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 21st, 2012, 10:19 PM
  4. returning 2d array in java
    By dr.code.skm in forum Member Introductions
    Replies: 2
    Last Post: July 20th, 2011, 10:14 AM
  5. returning a 2D array
    By straw in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 04:30 AM