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: counter in loop not incramenting when its suppose to

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

    Default counter in loop not incramenting when its suppose to

    I have to tell if someone passed a drivers test or not. for some reason in the class the
    correctCount
    isn't adding when it should so I am always having a return of false and I cant figure out why. 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]==correctAnswers[i])
                {
                   correctCount++;
                }
                else
                {
                   incorrectCount++;
                }
             }
     
             if(correctCount>14)
             {pass=true;}
             return pass;
     
          }
     
     
       }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,420
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: counter in loop not incramenting when its suppose to

    Please edit the post and properly format the code so that all the statements do not start in the first column.
    Logically nested statements within {}s should be indented 3-4 spaces to make reading easier.

    Use the equals() method when comparing Strings.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: counter in loop not incramenting when its suppose to

    Does your code even compile? If so, what are the errors?

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

    Default Re: counter in loop not incramenting when its suppose to

    it does compile the only issue I'm having is that my correctCount doesn't count up it stays at 0 so the return is always false

  5. #5
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: counter in loop not incramenting when its suppose to

    What I've been told is to not use the '==' operator when comparing Strings. Instead to use the equals method from the string class.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,420
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: counter in loop not incramenting when its suppose to

    Now for String comparison you need to replace the == with the equals() method.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: counter in loop not incramenting when its suppose to

    that was it thanks

Similar Threads

  1. Replies: 5
    Last Post: February 15th, 2013, 04:01 PM
  2. counter not incrementing within for loop - please help!!!
    By javaiscool in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 8th, 2013, 02:46 PM
  3. Button which suppose to hide JDialog..
    By dardam in forum AWT / Java Swing
    Replies: 3
    Last Post: April 7th, 2012, 03:55 PM
  4. Why does it get smaller when it's suppose to get bigger
    By velvetymold in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2011, 11:34 PM
  5. Help with counter controlled while loop please!
    By rockout341 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 15th, 2011, 02:41 PM