Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Jillian Skinner
Jillian Skinner
1,715 Points

Java if, else, switch help with code.

Thanks for the feedback. Here's the modified code, but now something else isn't right...I'm missing (learning) something...please advise. See the comments at the top to see what is should be doing. Thank you!!!!

import java.util.Scanner;
import static java.lang.System.out;
//Still doesn't work properly.  Needs some modification.
//---------PRICING------------
// VK, vk, Fridaydeal, fridaydeal, FAF, faf 7.25
// Avout, avout 4.12
// seniors/kiddos 5.25
// "regular" people, no coupon 9.25
//No double discounts for seniors/kiddos (no using coupon codes!)
//Seniors/kids who race for Avout, should use the Avout code since it's their best deal.

public class pricingWithDiscounts {

    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
        int age = 0;
        double price = 0.00;
        String code = null;
        char reply;

        out.print("Do you have a discount code? Y/N ");
        reply = keyboard.findWithinHorizon(".", 0).charAt(0);

        if (reply == 'Y' || reply == 'y') {
            out.print("Please enter your code. ");
            code = keyboard.next();
            switch (code) {
            case "VK":
            case "vk":
            case "Fridaydeal":
            case "fridaydeal":
            case "faf":
            case "FAF":
                out.print("Enter your age. ");
                age = keyboard.nextInt();
                if (age > 12 && age < 65) {
                    price = 7.25;
                } else {
                    price = 5.25;
                break;
                }
            case "Avout":
            case "avout":
                price = 4.12;
                break;          
                   }
            }
            else if (reply !='Y' && reply !='y'){
            out.print("Enter your age. ");
            age = keyboard.nextInt();
            }if (age <13 || age > 64){
                price = 5.25;
            }else {
                price = 9.25;
            }
        out.print("Your price is ");
        out.println(price);

        keyboard.close();
    }
} 

2 Answers

Allan Clark
Allan Clark
10,810 Points

Here is your bug:

if (age <13 && age > 64)

Missing the {

Hope this helps!

Ashley Livingston
Ashley Livingston
6,931 Points

Shouldn't it also be OR not AND

An age can't be both under 13 and over 64 at the same time.

Jillian Skinner
Jillian Skinner
1,715 Points

Thanks you two...I modified the code and reposted as I'm still misunderstanding something as the code still isn't working properly. :-( I'll get it eventually...with help! :-)

Ashley Livingston
Ashley Livingston
6,931 Points

Oh nos! I think you might be missing another } or two in there...

I put your code in my own workspace and made those changes...it compiles and works perfect for me. Here's my final code!

import java.util.Scanner;
import static java.lang.System.out;

public class pricingWithDiscounts {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        int age = 0;
        double price = 0.00;
        String code = null;
        char reply;

        out.print("Do you have a discount code? Y/N ");
        reply = keyboard.findWithinHorizon(".", 0).charAt(0);

        if (reply == 'Y' || reply == 'y') {
            out.print("Please enter your code. ");
            code = keyboard.next();

                switch (code) {
                case "VK":
                case "vk":
                case "Fridaydeal":
                case "fridaydeal":
                case "faf":
                case "FAF":
                    out.print("Enter your age. ");
                    age = keyboard.nextInt();
                    if (age > 12 && age < 65) {
                        price = 7.25;
                    } else {
                        price = 5.25;
                    }
                    break;

                case "Avout":
                case "avout":
                    price = 4.12;
                    break;          
                }
            }

            else if (reply !='Y' && reply !='y'){
            out.print("Enter your age. ");
            age = keyboard.nextInt();


            if (age <13 || age > 64) {
                price = 5.25;

            }else  {
                price = 9.25;
            }
            }




        out.print("Your price is ");
        out.println(price);

        keyboard.close();

    }
} 

edit: yep, it was just the closing } at the end of your else if!