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

Thread: Trouble with isolating a particular address in a for loop

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

    Default Trouble with isolating a particular address in a for loop

    OK, so I have to make a method that assigns a seat in an airplane. There are two options: First Class or Coach Class. I am having trouble with my reserveSeat() method. There are multiple ways to accomplish this but I am attempting to stick to the logic I have already provided. Here are all the methods, variables, objects defined already:

    -If the user chooses to reserve a seat (an integer of 0):
    if(choice == 0)
    			{
    				String classSpec = JOptionPane.showInputDialog("Specify Class type");
    				{
    					if(theSeat.checkSeatAvailability() == true)
    					{		
    						theAirplane.assignSeat(classSpec);
    						flag = true;
    					}
    					else
    						if(theSeat.checkSeatAvailability() == false)
    						{
    							JOptionPane.showMessageDialog( null,
    									"There are no more seats", 
    									"Seating." + " The Seat: " + theSeat.getSeatNum() + " is Occupied",
    									JOptionPane.INFORMATION_MESSAGE);
    							flag = true;
    						}
     
    				}
    				flag = true;
    			}
    __________________________________________________ __________________________________________________ __

    My reserveSeat method looks like this:
    public void assignSeat(String spec)
    	{
    		boolean flag = true;
    		if (spec.equals("First Class"))
    		{
    			 for(int i = 0; i < 4 && flag ; i++)
    			    {
    			            seatArray[i] = new Seat(i, spec);
    			            if(seatArray[i].checkSeatAvailability() == true)
    			            {
    			                seatArray[i].reserveSeat();
    			                flag = false;
    			            }
    			            else
    			            {
    			                flag = true;
    			            }
    			    }
    		}
     
    		else
    		{
     
    			for(int i = 4; i < 10 ; i++)
    			{
    				while(flag)
    				{
    					seatArray[i] = new Seat(i, spec);
    					if(seatArray[i].checkSeatAvailability() == true)
    					{
    						seatArray[i].reserveSeat();
    						flag = false;
    					}
    					else
    					{
    						flag = true;
    					}
    				}
    			}
    		}
     
    	}
    Last edited by pbrockway2; December 14th, 2012 at 03:16 PM. Reason: code tag added


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Trouble with isolating a particular address in a for loop

    Hi DamageInc, welcome to the forums!

    I have added "code" tags to your post. The idea is that you put [code] at the start of a section of code and [/code] at the end. That way the forum software renders the code in a readable way on the web page. (It also helps to use spaces rather than tabs to indent as the latter can be large, and to keep line length moderate. The old school 80 character limit grew to look a little silly as monitors grew, but it's starting to make a comeback now we read posts on small tablets and phones...)

    I am having trouble with my reserveSeat() method.
    So what's the problem? It will help to describe it precisely: does the code compile? If not, what are the compiler messages? Does it do something odd at runtime? (including throwing an exception) If so, what does it do, and what you expect or intend it would do? Code that is expected to compile and run would be nice - that way we can see the problem for ourselves - but this may involve removing irelevent stuff if your program is large.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with isolating a particular address in a for loop

    My code does compile, but it does not give me what I am expecting. I want to reserve a seat in one of the two classes(Coach or First), then reserve the next available seat in a class, until there is no more available seatings.

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

    Default Re: Trouble with isolating a particular address in a for loop

    if (spec.equals("First Class"))
    Using literals here ("first Class") is a dangerous way to code. If you have typed in the same literal in more than one place in the program there is a 100% chance that sometime in the many program you code you will mistype one and the if test that uses it will fail. The compiler will not help you find that kind of error, and you will spend a lot of time trying to find the spelling error.

    A better practice is to define a final variable with the value and then use that variable in the code.
    final String FirstClassNM = "FirstClass";
    ...
    spec = FirstClassNM;
    ...
    if (spec.equals(FirstClassNM))
    Then if you misspell the name of the variable, the compiler will give an error message.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Having trouble with for loop statement.
    By Truck35 in forum Loops & Control Statements
    Replies: 3
    Last Post: November 25th, 2012, 04:45 PM
  2. [SOLVED] Trouble with my while loop
    By masterhand89 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 17th, 2012, 12:45 PM
  3. Isolating a single word from user input...
    By ninjaBob in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2012, 04:29 PM
  4. Trouble Creating Instances with a While Loop
    By AbsolutelyNobody in forum Loops & Control Statements
    Replies: 3
    Last Post: October 13th, 2011, 07:20 AM
  5. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 03:25 AM

Tags for this Thread