Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java while problem

Options
  • 04-11-2008 1:37pm
    #1
    Registered Users Posts: 616 ✭✭✭


    I am working on a java assignment for my first year in computer science in ITB.

    At the moment I am haivng no trouble at all as most of my work is quite simple but for some reason at the end of the code(highlighted) there is a while that won't exit

    The code is as follows
    /******************************************************************/
    /* Java Program for weekend festival			          */
    /* Written by:   Sean O'Mahony                                    */
    /* Course: Computing                                              */
    /* Language:  Java                                                */
    /* Data started:14/10/2008                                 	  */
    /* Last update:14/10/2008                               	  */
    /* Program description:						  */
    /******************************************************************/
    
    class project
    {
    	public static void main(String[] args)
    	{
    
    
    		int number_ticket, ticket_type;
    		double cost=0;
    		char go_again;
    		String name;
    
    
    		do
    		{
    			System.out.println("***************************************************************");
    			System.out.println("*                                                             *");
    			System.out.println("*   1). Day Ticket  96.50 Euro Per Person                     *");
    			System.out.println("*   2). Weekend ticket with camping 224.50 Euro Per person    *");
    			System.out.println("*   3). Weekend ticket no camping 177.50 Euro per person      *");
    			System.out.println("*                                                             *");
    			System.out.println("***************************************************************");
    
    			System.out.println("Please enter the type of ticket you would like");
    			ticket_type=Keyboard.readInt();
    
    			while(ticket_type<1||ticket_type>3)
    			{
    				System.out.println("Invalid number, please enter a number between 1 and 3");
    				System.out.println("Plese try again");
    				ticket_type=Keyboard.readInt();
    			}
    			switch(ticket_type)
    			{
    				case 1:cost=96.50;  break;
    				case 2:cost=224.50; break;
    				case 3:cost=177.50;
    			}  //end of switch
    
    			System.out.println("Please enter your surname");
    			name=Keyboard.readString();
    			System.out.println("Please enter the number of tickets you which to purchase");
    			System.out.println("The max amount is 4");
    			number_ticket=Keyboard.readInt();
    
    			while(number_ticket<1||number_ticket>4)
    			{
    				System.out.println("Invalid number, maximum number of tickets is 4");
    				System.out.println("Please try again");
    			}  //end of while loop
    
    			cost=cost*number_ticket;
    
    			System.out.println("Festivle Ticket Type= " + ticket_type);
    			System.out.println("Surname= " + name);
    			System.out.println("No in Party= " + number_ticket);
    			System.out.println("Total cost= " + cost);
    
    			System.out.println("Would you like to go again?");
    			go_again=Keyboard.readChar();
    			go_again=Character.toUpperCase(go_again);
    
    			while(go_again!='Y'||go_again!='N')
    			{
    				System.out.println("Invalid, please enter either Y or N");
    				System.out.println("Would you like to go again?");
    				go_again=Keyboard.readChar();
    			}  // end of while
    
    		}while(go_again=='Y');  //end of while
    
    		System.out.println("Thank you and goodbye");
    
    	}  //end of main
    
    }  //end of class
    


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    while(go_again!='Y'||go_again!='N')

    That means if you press Y then, you get boolean 0 || 1 which = 1 so your loop will be true even if you do press Y. What you should do is

    while(go_again!='Y' && go_again!='N')

    Now you get 0 && 1 for a Y pressed which is false, (exit while) which is what you want and vice versa if its N.

    Also note that you should do a toUpper inside the while loop too as you are over writing the previous one outside the loop.
    while(go_again!='Y' && go_again!='N')
    			{
    				System.out.println("Invalid, please enter either Y or N");
    				System.out.println("Would you like to go again?");
                                    go_again = Character.toUpperCase(Keyboard.readChar());
    			}  // end of while
    


  • Registered Users Posts: 616 ✭✭✭DisasterIRL


    Webmonkey wrote: »
    while(go_again!='Y'||go_again!='N')

    That means if you press Y then, you get boolean 0 || 1 which = 1 so your loop will be true even if you do press Y. What you should do is

    while(go_again!='Y' && go_again!='N')

    Now you get 0 && 1 for a Y pressed which is false, (exit while) which is what you want and vice versa if its N.

    Also note that you should do a toUpper inside the while loop too as you are over writing the previous one outside the loop.


    Thank you very much my fine Sir!

    I don't know why but that never crossed my mind. Thanks again and amazing responce speed


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    No Probs, fixed code there, little typo.


  • Closed Accounts Posts: 307 ✭✭Idgeitman


    the minute i saw something to do with tickets and being a small program, it reminded me of the one I made when I started out in college, and turns out you were also in ITB :P


  • Registered Users Posts: 70 ✭✭Creadak


    while(go_again=='Y');

    Uhm... you said you'd fixed your code, but does that semi-colon there not create an infinite while loop?


  • Advertisement
  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Creadak wrote: »
    while(go_again=='Y');

    Uhm... you said you'd fixed your code, but does that semi-colon there not create an infinite while loop?
    Its a do while loop hince you need the semi colon there at end to end the statement. Common mistake to forget this.


  • Registered Users Posts: 616 ✭✭✭DisasterIRL


    Webmonkey wrote: »
    Its a do while loop hince you need the semi colon there at end to end the statement. Common mistake to forget this.

    Even I knew that:P:D


Advertisement