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: issue with return values stuck at 0

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

    Default issue with return values stuck at 0

    I'm writing this program that is a drivers license test . the main method allows the user to input 20 answers and it creates an array of those answers then it is suppose to make that a new instance of a class that checks the array of the users answers against an array of the correct answers and return true or false to tell if the user passed then I need a method to return the number of correct answers and a method to return the number of incorrect answers. My issue is that my methods for correct and incorrect are always returning 0 and the Boolean method I always false. Thanks

    public class DriverExam2
       {
          private String[] correctAnswers={"b","d","a","a","c","a","b","a","c","d","b","c","d","a","d","c","c","b","d","a"};
          private String[] userAnswers=new String[20];
          boolean passed;
          int numberCorrect;
          int numberIncorrect;
     
          //constructer to set userAnswers
          public DriverExam2(String[] entered)
          { userAnswers=entered;
          }
     
          //accessor method to return userAnswers
          public String[] getUserAnswers()
          {
             return userAnswers;
          }
     
          //mutator method to assign count and pass fail
          public void setInfo()
          {
             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;}
             passed=pass;
             numberCorrect=correctCount;
             numberIncorrect=incorrectCount;
          }
     
             //accessor methods to return if you passed,the correct count, and the incorrect count.
          public boolean getPassFail()
          {
             return passed;
          }
     
          public int getTotalCorrect()
          {
             return numberCorrect;
          }
     
          public int getTotalIncorrect()
          {
             return numberIncorrect;
          }
     
       }


       import java.util.Scanner;
       public class DriverExamDemo2
       {
          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
             DriverExam2 exam1 = new DriverExam2(userAnswers);
     
          //call the accessor methods for total correct total incorrect and 
             System.out.println("the number of questions you answered correctly was "+exam1.getTotalCorrect());
             System.out.println("the number of questions you answered incorrectly was "+exam1.getTotalIncorrect());
     
          //get the boolean return from pass fail method and print out accordingly to wether it is true or false
             if(exam1.getPassFail())
                System.out.println("you passed your driving exam");
             else
                System.out.println("you failed your driving exam");
     
     
     
          }
       }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: issue with return values stuck at 0

    Boolean method I always false. T
    What is the name of the method that always returns false?


    Make sure there are no local variables that hide class variables.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: issue with return values stuck at 0

    It was the getPassFail method but I figured it out I forgot to call the Exam1.setInfo() method in the main method so the variables I was trying to return were never set. Thanks

Similar Threads

  1. how to get return values from a button to main program.
    By harshilshah in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2013, 07:17 AM
  2. [SOLVED] Return all values in an ArrayList from a method
    By IanSawyer in forum Collections and Generics
    Replies: 1
    Last Post: March 28th, 2012, 09:53 AM
  3. Need help my values wont return
    By sajeed in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 7th, 2012, 07:46 AM
  4. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:24 AM
  5. Return the rgb values from a jpg image
    By newparticipant in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 25th, 2011, 03:47 PM

Tags for this Thread