Results 1 to 8 of 8
  1. #1
    jmrire is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Question Help with exception and loop

    Hello i'm using Textpad.
    I have a class and an application. in my class i'm not sure what i'm missing for it to go from
    if there is no seat in club you can book coach and vis versa.
    my program skips that part and does the " there are no more coach /club seat take the next train.
    Java Code:
    mport javax.swing.JOptionPane;
    import java.util.*;
    
    public class railreservation3
    {
    
    
    	 	String  name ;// up to 6 names
    
            int [] club = new int[3];
            int [] coach = new int [3];
            int moreseats = 1;
            int clubindex=0;
            int coachindex=0;
            String struserinput;
            String strmoreseats;
            int moreseat;
            int userinput;
    
    
    
    	public void reserve()
    
    	{
            for (int index =0; index <3; index++)
            {
    
                    club [index] =0;
                    coach[index]=0;
            }
        do
        {
            try
            {
    			JOptionPane.showMessageDialog(null,"Welcome to Fun Rail Reservations.");
    			name=JOptionPane.showInputDialog(null,"Enter Passenger name");
                struserinput =JOptionPane.showInputDialog(null," Please enter 1 for Club car seating.\n"
                        + "  Please enter 2 for Coach car seating.\n Or enter 0 to cancel");
                userinput= Integer.parseInt (struserinput);
    
    
                    if(userinput==1)
                        if(club[clubindex]==0)
                        {
                            club[clubindex]=1;
    
                  JOptionPane.showMessageDialog(null,"Passenger name:  " + name + " \nYour seat number is:  " +(clubindex+1) + "\nConfirmed class: One Club car seat.");
                      clubindex++;
                      }
                    if (userinput==2)
                        if (coach[coachindex]==0)
                        {
                            coach[coachindex]=1;
    
                    JOptionPane.showMessageDialog(null,"Passenger name:  "+ name + "\nYour seat number is:  " +(coachindex+4) +"\nConfirmed class: One Coach car seat.");
                        coachindex++;
                        }
                    if(userinput>2)
                        JOptionPane.showMessageDialog(null,"must enter only 1 or 2 ");
                                if (userinput == 0)
                                    break;
                }
            catch (NumberFormatException numberFormatException)
            {
                JOptionPane.showMessageDialog(null,"You must enter an integers 1 for Club, 2 for Coach");
            }
            catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException)
            {
                if ((clubindex>3) && (coachindex>3))
                {
    						//if(clubindex==3)
    
                  			 //moreseats=0;
                   strmoreseats=JOptionPane.showInputDialog(null," Club car is full, if you like you can reserver a Coach car.\n for $100 less"
                  										  +"If you want to proceed press 1 to cancel press 0");
                moreseats= Integer.parseInt (strmoreseats);
    
                			//if(moreseats==1)
    
    
    
    
                        break;
                    }
                if(clubindex==3)
                    JOptionPane.showMessageDialog(null,"No more Club car seat. Sorry , try another train");
                if(coachindex==3)
                    JOptionPane.showMessageDialog(null,"No more Coach car seat. Sorry, Try another train");
    
          }
        }
            while(moreseats==1);
        }
    
    } //end class
    Last edited by Norm; 03-03-2012 at 02:07 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Help with exception and loop

    my program skips that part
    Why does it skip that part? What is there in the code that makes it skip it?
    Add some println statements to print out the values of the variables that control where the code executes.
    The print out should show you what the code is doing and what it is looking at so you can understand what it is doing.

  3. #3
    endotherm is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help with exception and loop

    I know this doesn't directly answer your question, but it is generally considered bad form to use exception handling to handle flow control decisions.
    its certianly easy enough to check the size/fullness of the array before attempting to add an element, and making a decision if it is full. try to keep catch block code to activities like recovering, logging, or notifying.

    also try to avoid break statements. they can have unexpected behavior. instead simply 'return;'

    now, in reference to your code, before you show your message, you make this check:
    Java Code:
     if ((clubindex>3) && (coachindex>3))
    should I interpret that to mean 'if both the club and coach class are full'? I may be misunderstanding, but it seems like you need to change the && (and) to an || (or), indicating that only one of the two classes is full. sorry if I'm misunderstanding your intent.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Help with exception and loop

    misunderstanding your intent.
    Some comments in the code could show what the OP intends the code to do. We can read the code and see what it does, but without comments we can't know what the OP wants the code to do.

  5. #5
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default Re: Help with exception and loop

    Hi,

    I am a newcomer in this forum do not know how and where to post my problems and to whom I should ask my question! Could any one please help me out?

    Sincerely,

    Zobair
    id name javaprogrammer

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,381
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help with exception and loop

    On the thread listing page there's a button near the top left of the page 'post new thread'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default Re: Help with exception and loop

    Sorry! I still don't find where it is writen "post new thread" if you do not mind can you give me the link of thread list?

    sincerely,

  8. #8
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default Re: Help with exception and loop

    Thanks a lot I have just got it. Thank you very much indeed.

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  3. Catch exception, return to while loop
    By ChicagoAve in forum New To Java
    Replies: 3
    Last Post: 02-20-2011, 06:44 PM
  4. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •