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

Thread: Help bolean method and while loop !!!!

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help bolean method and while loop !!!!

    Good afternoon, everyone I have a problem with my program. My last method (isValid) checks if the input is valid from 1 to 100 when I call the method with the while loop in the first input it works fine. but in the second input it wont work you can try to enter 101 and it would continue to the 3rd input I dont know whats wrong! in the input #2 i changed the while loop to compare it to score2 which is input 2 but nothing it wont check if its a valid input








    PHP Code:
    import java.util.Scanner;


    public class 
    TestAverageAndLetterGrade 
    {
        public static 
    void main(String[] args)            
        {
                
        
    Scanner keyboard = new Scanner(System.in);
            
        
    int score1;
        
    int score2;
        
    int score3;
        
    int score4;
        
    int score5;

        
    System.out.println("Enter your 1st test score: ");
        
    score1 keyboard.nextInt();
            
    boolean isValid isValid(score1);
            
            while (
    isValid != true )
            {
                
    System.out.println("Enter correct Test score: ");
                
    score1 keyboard.nextInt();
                
    isValid isValid(score1);
            }
            
        
    System.out.println("Enter your 2nd test score: ");
        
    score2 keyboard.nextInt();

            while (
    isValid != true )
            {
                
    System.out.println("Enter correct Test score: ");
                
    score2 keyboard.nextInt();
                
    isValid isValid(score2);         
            }
            
        
    System.out.println("Enter your 3rd test score: ");
        
    score3 keyboard.nextInt();
            
            while (
    isValid != true )
            {
                
    System.out.println("Enter correct Test score: ");
                
    score3 keyboard.nextInt();
                
    isValid isValid(score3);         
            }
            
        
    System.out.println("Enter your 4th test score: ");
        
    score4 keyboard.nextInt();
            
            while (
    isValid != true )
            {
                
    System.out.println("Enter correct Test score: ");
                
    score4 keyboard.nextInt();
                
    isValid isValid(score4);         
            }
            
        
    System.out.println("Enter your 5th test score: ");
        
    score5 keyboard.nextInt();
            
            while (
    isValid != true )
            {
                
    System.out.println("Enter correct Test score: ");
                
    score5 keyboard.nextInt();
                
    isValid isValid(score5);         
            }
            
            
    System.out.println("=========================================");
            
            
    double average calculateAverage(score1score2score3score4score5);

            
        
    System.out.println (" The average of the 5 test scores is: " gradeLetter(average));
        
    gradeLetter(average);
            
        
    System.out.print(" test #1");
            
    gradeLetter(score1);
            
    System.out.print(" test #2");
            
    gradeLetter(score2);
            
    System.out.print(" test #3");
            
    gradeLetter(score3);
            
    System.out.print(" test #4");
            
    gradeLetter(score4);
            
    System.out.print(" test #5");
            
    gradeLetter(score5);

        
    }
        public static 
    double calculateAverage(int score1
                                              
    int score2
                                              
    int score3
                                              
    int score4
                                              
    int score5
        {
            
    double average = (score1 score2 score3 score4 score5) / 5;
            return 
    average;
            
        }
        public static 
    double gradeLetter(double average)
        {
            if (
    average>90)
            {
                
    System.out.println(" You received an A :) ");
            }
            else if (
    average>=80)
            {
                
    System.out.println(" You received a  B");
            }
            else if (
    average>=70)
            {
                
    System.out.println(" You received a  C ");
            }
            else if (
    average>=60)
            {
                
    System.out.println(" You received a  D");
            }
            else if (
    average<60)
            {
                
    System.out.println(" You received an F :( ");
            }
                    return 
    average;
        }
             public static 
    boolean isValid(int score)
                    
                    
            {
                
    boolean status;
                
    status true;
                
                if(
    score  >= && score <= 100);
                     if(
    score  >= && score <= 100);
                    
    // if(score3  >= 1 && score3 <= 100);
                         //   if(score4  >= 1 && score4 <= 100);
                           //     if(score5  >= 1 && score5 <= 100);
                
                     
                
    else
                
    status false;
                return 
    status;
            
            }

    Last edited by pip0; March 30th, 2012 at 01:35 PM.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Help bolean method and while loop !!!!

    here is the code i fixed you need to put isValid(score2) before the second while loop executes and do this for all if you copy and paste this code it should work. I also dont think you need isValid(score2) and so forth inside your while loops.just

    ...edited by moderator
    Last edited by copeg; March 30th, 2012 at 04:57 PM.

  3. The Following User Says Thank You to NewAtJava For This Useful Post:

    pip0 (March 30th, 2012)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Help bolean method and while loop !!!!

    Is your problem solved then?

  5. #4
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Help bolean method and while loop !!!!

    his problem is solved then

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Help bolean method and while loop !!!!

    Quote Originally Posted by NewAtJava View Post
    his problem is solved then
    I did not realize another user posted that response. NewAtJava, I highly recommend reading the forum rules, as well as the following:
    The Problem with Spoon-feeding

  7. #6
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Help bolean method and while loop !!!!

    yes i understand but i wish someone would spoon feed me sometimes so i could study there code and learn. To bad know one spoon feeds me!

  8. #7
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Help bolean method and while loop !!!!

    true spoon feeding is not always helpful but in my case It would have been cause java programming forums is my last choice after i have studied and checked every possible possibility for my java problems so a spoon fed answer is not bad sometimes.

  9. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Help bolean method and while loop !!!!

    Quote Originally Posted by NewAtJava View Post
    true spoon feeding is not always helpful but in my case It would have been cause java programming forums is my last choice after i have studied and checked every possible possibility for my java problems so a spoon fed answer is not bad sometimes.
    If you read the link, you would understand why there is a forum policy and why spoonfeeding is frowned upon. It robs you of learning how to problem solve, and if you cannot learn how to problem solve then you're programming skills will never improve. If you wish to discuss this elsewhere that is fine, but let's please not hijack someone else's post.

  10. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help bolean method and while loop !!!!

    Quote Originally Posted by NewAtJava View Post
    here is the code i fixed you need to put isValid(score2) before the second while loop executes and do this for all if you copy and paste this code it should work. I also dont think you need isValid(score2) and so forth inside your while loops.just

    ...edited by moderator

    Thanks for your help NewAtJava and not thinking your some hot *** ! I tried what you told me but nothing I put isValid(score2) and so on for all the inputs but the method doesn't seem to work only for the firs input..... and as for the isValid in isValid = isValid(score2); in the while loop if i were to take that out wouldn't it be an infinite loop. heres the code re edited idk if that how you meant the method should be called


    PHP Code:
    import java.util.Scanner;


    public class 
    TestAverageAndLetterGrade 
    {
        public static 
    void main(String[] args)            
        {
                
        
    Scanner keyboard = new Scanner(System.in);
            
        
    int score1;
        
    int score2;
        
    int score3;
        
    int score4;
        
    int score5;

        
    System.out.println("Enter your 1st test score: ");
        
    score1 keyboard.nextInt();
            
    boolean isValid isValid(score1);
            
            while (
    isValid != true )
            {
                
    System.out.println("Enter correct Test score: ");
                
    score1 keyboard.nextInt();
                
    isValid isValid(score1);
            }
            
        
    System.out.println("Enter your 2nd test score: ");
        
    score2 keyboard.nextInt();
            
            
    isValid(score2);

            while (
    isValid != true )
            {
                
    System.out.println("Enter correct Test score: ");
                
    score2 keyboard.nextInt();
                
    isValid isValid(score2);         
            }
            
        
    System.out.println("Enter your 3rd test score: ");
        
    score3 keyboard.nextInt();
            
            
    isValid(score3);
            
            while (
    isValid != true )
            {
                
    System.out.println("Enter correct Test score: ");
                
    score3 keyboard.nextInt();
                
    isValid isValid(score3);         
            }
            
        
    System.out.println("Enter your 4th test score: ");
        
    score4 keyboard.nextInt();
            
            
    isValid(score4);
            
            while (
    isValid != true )
            {
                
    System.out.println("Enter correct Test score: ");
                
    score4 keyboard.nextInt();
                
    isValid isValid(score4);         
            }
            
        
    System.out.println("Enter your 5th test score: ");
        
    score5 keyboard.nextInt();
            
            
    isValid(score5);
            
            while (
    isValid != true )
            {
                
    System.out.println("Enter correct Test score: ");
                
    score5 keyboard.nextInt();
                
    isValid isValid(score5);         
            }
            
            
    System.out.println("=========================================");
            
            
    double average calculateAverage(score1score2score3score4score5);

            
        
    System.out.println (" The average of the 5 test scores is: " gradeLetter(average));
        
    gradeLetter(average);
            
        
    System.out.print(" test #1");
            
    gradeLetter(score1);
            
    System.out.print(" test #2");
            
    gradeLetter(score2);
            
    System.out.print(" test #3");
            
    gradeLetter(score3);
            
    System.out.print(" test #4");
            
    gradeLetter(score4);
            
    System.out.print(" test #5");
            
    gradeLetter(score5);

        
    }
        public static 
    double calculateAverage(int score1
                                              
    int score2
                                              
    int score3
                                              
    int score4
                                              
    int score5
        {
            
    double average = (score1 score2 score3 score4 score5) / 5;
            return 
    average;
            
        }
        public static 
    double gradeLetter(double average)
        {
            if (
    average>90)
            {
                
    System.out.println(" You received an A :) ");
            }
            else if (
    average>=80)
            {
                
    System.out.println(" You received a  B");
            }
            else if (
    average>=70)
            {
                
    System.out.println(" You received a  C ");
            }
            else if (
    average>=60)
            {
                
    System.out.println(" You received a  D");
            }
            else if (
    average<60)
            {
                
    System.out.println(" You received an F :( ");
            }
                    return 
    average;
        }
             public static 
    boolean isValid(int score)
                    
                    
            {
                
    boolean status;
                
    status true;
                
                if(
    score  >= && score <= 100);
                     if(
    score  >= && score <= 100);
                    
    //    if(score3  >= 1 && score3 <= 100);
                         //   if(score4  >= 1 && score4 <= 100);
                           //     if(score5  >= 1 && score5 <= 100);
                
                     
                
    else
                
    status false;
                return 
    status;
            
            }

    Last edited by copeg; March 31st, 2012 at 07:05 PM.

  11. #10
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Help bolean method and while loop !!!!

    Hello pip0!
    What do you need the boolean variable isValid for? You can test directly your isValid(int score) method in the while loops since it returns a boolean value. To be more clear:
    instead of
    boolean isValid = isValid(score1);
    while (isValid != true )
    you can just do this:
    while (!isValid(score1))
    Hope it helps.

  12. #11
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Help bolean method and while loop !!!!

    I ran your program on my computer and it worked fine with copy and paste the one i left on there for you. It worked on mine perfectly every time i put a higher number than 100 it said enter correct number and when i put the right one in it moved on correctly. I tried it again it works perfect!

  13. The Following User Says Thank You to NewAtJava For This Useful Post:

    pip0 (April 2nd, 2012)

  14. #12
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Help bolean method and while loop !!!!

    here it is again hope this helps I know how it can be to figure out a program problem its frustrating sometimes!

    ...edited by moderator
    Last edited by copeg; March 31st, 2012 at 07:01 PM.

  15. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Help bolean method and while loop !!!!

    @NewAtJava, you've been warned, and this time issued an infraction for violating forum rules. I'm a very patient moderator, but your last post completely disregarded my previous warning. One more and you will be banned.

  16. #14
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Help bolean method and while loop !!!!

    Quote Originally Posted by pip0 View Post
    Thanks for your help NewAtJava and not thinking your some hot *** ! I tried what you told me but nothing I put isValid(score2) and so on for all the inputs but the method doesn't seem to work only for the firs input..... and as for the isValid in isValid = isValid(score2); in the while loop if i were to take that out wouldn't it be an infinite loop. heres the code re edited idk if that how you meant the method should be called
    First, please watch you're language. Second, there is a big difference between calling this
    boolean isValid = isValid(score1);

    vs:

    isValid(score2);

    The first (used for your first validation) actually uses the returned value in subsequent code, the second (used for the other validations) does not.

  17. #15
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help bolean method and while loop !!!!

    Quote Originally Posted by copeg View Post
    First, please watch you're language. Second, there is a big difference between calling this
    boolean isValid = isValid(score1);

    vs:

    isValid(score2);

    The first (used for your first validation) actually uses the returned value in subsequent code, the second (used for the other validations) does not.
    Okay " Super Moderator " so can you please explain what I'm doing wrong and have some patient for the people who are trying to learn! not everyone knows it all ! I don't want to be spoon fed I just want to understand and learn.

  18. #16
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Help bolean method and while loop !!!!

    Quote Originally Posted by pip0 View Post
    Okay " Super Moderator " so can you please explain what I'm doing wrong and have some patient for the people who are trying to learn! not everyone knows it all ! I don't want to be spoon fed I just want to understand and learn.
    I must be missing something...what about enforcing rules, helping run these forums, and helping people in my valuable spare time relates to not having patience, and deserving of a reply like that? You don't pay for this service, and you should consider yourself lucky that forums such as this exist - and exist because of moderators and contributors such as myself who donate their time.

    This thread isn't about enforcing rules, its about helping you...and I presume you don't want this thread to side-track into another topic entirely.

    What exactly did you not understand about my post above, the one which you quoted? Think about it this way
    boolean isValid = isValid(score1);//suppose this call to isValid returns true
    if ( isValid ){
        //do something
    }
    isValid(score2);//suppose this call to isValid returns false
    if ( isValid ){//the variable isValid was never set by the call in the above line, so it will have the same value as when it was last set. 
     
    }

Similar Threads

  1. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 07:57 PM
  2. [SOLVED] Method to reverse String using While Loop
    By Montrell79 in forum Loops & Control Statements
    Replies: 2
    Last Post: February 22nd, 2012, 02:49 PM
  3. Challenge: I need an animation loop outside of the paint method.
    By Spidey1980 in forum Java Theory & Questions
    Replies: 28
    Last Post: August 20th, 2011, 09:20 AM
  4. POST method in a loop
    By kollyisrealisaac in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: July 26th, 2011, 11:29 AM
  5. private method , loop , switch , help?
    By wolfgar in forum Loops & Control Statements
    Replies: 9
    Last Post: November 8th, 2009, 10:04 PM