Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Help needed. y / n

    Hi,

    I have the following code working as I need it to but I am having to main problems that I cannot figure out, and I have spent a lot of time on this. As you have probably guessed I am a complete noob!!

    I need the following code to only accept y/n as an answer. If yes the program repeats, if no it exits and prints details to screen. Currently if the user enters any other character other than y/n the program prints the details and terminates.

    I have tried using if(!answer.equals("y")||!answerequals("n")); but it wont work.
    If I try using an else statement it continually returns the "else without if" error.

    I'm completely stuck on this and any help /point in the right direction would be greatly appreciated.





    Java Code:
    *This program reads  details from a user and outputs those details in the form of a ticket.*/
    
    			}
    
    			while (again.equals ("y"));
    
    
    
    			if (again.equals ("n"));
    
    		
    	}
    }
    Problem resolved
    Last edited by Theringmaster26; 11-08-2012 at 02:07 PM. Reason: Problem resolved

  2. #2
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help needed. y / n

    **I have tried using the else statement at line 72, it keeps giving me the "else without if" error.

  3. #3
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Help needed. y / n

    You have your brackets messed up at line 59. There is a semi colon at the end of the line, so the if ends there. It should look like this:
    Java Code:
            
    if (again.equals("n")) {
         System.out.println("HERE ARE YOUR TICKET DETAILS");
         <snip>
    }
    Edit: the reason that this works is that you can add { and } (called a block) anywhere in your code. So you had an if-statement that didn't do anything, and a block that printed the details.
    Last edited by SurfMan; 11-08-2012 at 11:33 AM.

  4. #4
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help needed. y / n

    Thank you very much Surfman. I don't know how long I spent staring at the above program.

    All I have to do now is figure out how to get the program to take the correct response, after a prompt if incorrect imput, and re start the loop.

    Once again,
    Thank you.

  5. #5
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Help needed. y / n

    No problem, glad to help.

    Here's some more hints for ya:
    - You can move the printing of the details out of the if ( party > 10) block, since it's the same for both cases and only the costs change. You can then also move the again = EasyIn.getString() out of it as well.
    - Your choice for a do/while loop works, but you have to remember that this loop runs for as long as the response is "y". If the user presses "x", the while-loop will terminate. Maybe that's not what you want.

  6. #6
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help needed. y / n

    I need the program to do the folowing, and it's truely begining to get the better of me here !!

    -Take input from user.
    -Apply a discount if more than x amount of people
    -Full price if less than x amount of people
    -Ask user if the want another ticket
    -If yes, repeat loop
    -If no, terminate program and print details and number of tickets processed to screen.
    -I need the program to ONLY accept a y / n, if anything else is entered a "please only enter y / n" message appears and the user can then enter y / n and the loop restarts.

    I'be tried several varaiations and Im also mindful of not putting illegable code up here so trying to keep it simple !!!!

  7. #7
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Help needed. y / n

    You are definately on the right track here. What helps, is before you begin, try to write this down in human language. Some things I have left out, but you get my idea:

    party = how many people are in the party (input from the user)
    cost = party times 35
    if the party is greater than 10
    then apply discount to cost (I see you charge a party greater than 10 each €15. Is that correct?)
    print the details

    After that, you can translate it into Java code:
    Java Code:
    ...
    int party = EasyIn.getInt();
    int cost = party * 35
    if ( party > 10 ) {
       cost = party * 15;
    }
    ...
    or the same, but written a bit differently:
    Java Code:
    ...
    int party = EasyIn.getInt();
    int cost = 0;
    if ( party > 10 ) {
       cost = party * 15
    }
    else {
       cost = party * 35;
    }
    ...
    Last edited by SurfMan; 11-08-2012 at 12:31 PM.

  8. #8
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

Similar Threads

  1. little help needed over here.
    By OtNePx in forum Networking
    Replies: 11
    Last Post: 02-02-2012, 01:32 PM
  2. Help Needed^^
    By hitech83 in forum New To Java
    Replies: 5
    Last Post: 01-24-2011, 10:08 PM
  3. Help needed
    By amirsydney in forum JDBC
    Replies: 3
    Last Post: 11-24-2010, 01:39 PM
  4. help needed
    By amarchandar in forum New To Java
    Replies: 1
    Last Post: 02-18-2009, 12:38 PM
  5. Help needed
    By harshalsakpal in forum CLDC and MIDP
    Replies: 0
    Last Post: 02-17-2009, 04:53 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
  •