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

Thread: Why is the condition not called from method...

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why is the condition not called from method...

    Hi,
    I am learning core java fundamentals, experimenting with the logic and getting a hold of the programming concepts. I have written a simple code having a Flight class and trying to understand why the "if (passengers>seats)", condition does not get called and the message does not print on screen from handleTooMany() method when i create new passenger instances in main, where count is > 250. Here is the code:

    public class Main {

    public static void main(String[] args) {
    Flight Flight2 = new Flight();
    int i;
    for (i=0; i<255; i++){
    Flight2.add1Passenger();
    }


    System.out.println("No., of passengers = "+ Flight2.passengers);

    }


    public static class Flight {

    int passengers;
    int seats;

    Flight(){
    passengers=0;
    seats=250;
    }

    public void add1Passenger(){
    if (passengers<seats)
    passengers +=1;
    //System.out.println("No..., of passengers = "+ passengers);

    else handleTooMany();
    }

    private void handleTooMany(){
    if (passengers>seats)
    System.out.println("Too many passengers. Passengers exceed seats...");

    }
    }
    }

    Thank you.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why is the condition not called from method...

    Moved to the "What's wrong..." section.

    Be sure to wrap any code in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to block the JSP being called by GET method
    By aswinraj11 in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: May 23rd, 2014, 12:40 AM
  2. Method returns empty stack when called
    By pyler in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 7th, 2013, 05:46 PM
  3. equals method is never called
    By justme1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2013, 11:39 PM
  4. toggle colors from an array when the method is called.
    By samjoyboy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 06:00 PM