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
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Checkout bug

  • 15-10-2013 6:38pm
    #1
    Registered Users Posts: 175 ✭✭


    I'm doing a current assessment in software development in which we have to make a checkout that lets a user input three groceries and their amounts and based on the total cost administers a discount of either 5 or 10%. everything else in the program seems to work and it compiles but it skips asking for the second grocery and goes straight to the third. I'm only in my first year of software development by the way
    ==========================================================
    import java.util.Scanner;
    public class CheckoutSwitch{
    public static void main(String[] args)
    {
    Scanner in = new Scanner (System.in);

    double totalCost;
    double fiveDiscount;
    double tenDiscount;

    double app = 0.30;
    double ora = 0.45;
    double str = 2.30;
    double pot = 3.25;
    double tur = 0.70;
    double car = 1.25;

    int apples;
    int oranges;
    int strawberries;
    int potatoes;
    int turnips;
    int carrots;

    String grocery1;
    double grocery1amt = 0;

    String grocery2;
    double grocery2amt = 0;

    String grocery3;
    double grocery3amt = 0;


    System.out.println ("Please enter your first grocery: ");
    grocery1 = in.nextLine();
    if (grocery1.equalsIgnoreCase("apples"))
    {
    System.out.println("How many Apples would you like? ");
    apples = in.nextInt();
    grocery1amt = apples * app;

    }
    else if (grocery1.equalsIgnoreCase("oranges"))
    {
    System.out.println("How many Oranges would you like? ");
    oranges = in.nextInt();
    grocery1amt = oranges * ora;
    }
    else if (grocery1.equalsIgnoreCase("strawberries"))
    {
    System.out.println("How many Strawberries would you like? ");
    strawberries = in.nextInt();
    grocery1amt = strawberries * str;
    }
    else if (grocery1.equalsIgnoreCase("potatoes"))
    {
    System.out.println("How many Potatoes would you like? ");
    potatoes = in.nextInt();
    grocery1amt = potatoes * pot;
    }
    else if (grocery1.equalsIgnoreCase("turnips"))
    {
    System.out.println("How many Turnips would you like? ");
    turnips = in.nextInt();
    grocery1amt = turnips * tur;
    }
    else if (grocery1.equalsIgnoreCase("carrots"))
    {
    System.out.println("How many Carrots would you like? ");
    carrots = in.nextInt();
    grocery1amt = carrots * car;
    }


    // second grocery
    {
    System.out.println ("Please enter your second grocery: ");
    grocery2 = in.nextLine();
    }
    if (grocery2.equalsIgnoreCase("apples"))
    {
    System.out.println("How many Apples would you like? ");
    apples = in.nextInt();
    grocery2amt = apples * app;
    }
    else if (grocery2.equalsIgnoreCase("oranges"))
    {
    System.out.println("How many Oranges would you like? ");
    oranges = in.nextInt();
    grocery2amt = oranges * ora;
    }
    else if (grocery2.equalsIgnoreCase("strawberries"))
    {
    System.out.println("How many Strawberries would you like? ");
    strawberries = in.nextInt();
    grocery2amt = strawberries * str;
    }
    else if (grocery2.equalsIgnoreCase("potatoes"))
    {
    System.out.println("How many Potatoes would you like? ");
    potatoes = in.nextInt();
    grocery2amt = potatoes * pot;
    }
    else if (grocery2.equalsIgnoreCase("turnips"))
    {
    System.out.println("How many Turnips would you like? ");
    turnips = in.nextInt();
    grocery2amt = turnips * tur;
    }
    else if (grocery2.equalsIgnoreCase("carrots"))
    {
    System.out.println("How many Carrots would you like? ");
    carrots = in.nextInt();
    grocery2amt = carrots * car;
    }


    // third grocery


    System.out.println ("Please enter your third grocery: ");
    grocery3 = in.nextLine();
    if (grocery3.equalsIgnoreCase("apples"))
    {
    System.out.println("How many Apples would you like? ");
    apples = in.nextInt();
    grocery3amt = apples * app;

    }
    else if (grocery3.equalsIgnoreCase("oranges"))
    {
    System.out.println("How many Oranges would you like? ");
    oranges = in.nextInt();
    grocery3amt = oranges * ora;
    }
    else if (grocery3.equalsIgnoreCase("strawberries"))
    {
    System.out.println("How many Strawberries would you like? ");
    strawberries = in.nextInt();
    grocery3amt = strawberries * str;
    }
    else if (grocery3.equalsIgnoreCase("potatoes"))
    {
    System.out.println("How many Potatoes would you like? ");
    potatoes = in.nextInt();
    grocery3amt = potatoes * pot;
    }
    else if (grocery3.equalsIgnoreCase("turnips"))
    {
    System.out.println("How many Turnips would you like? ");
    turnips = in.nextInt();
    grocery3amt = turnips * tur;
    }
    else if (grocery3.equalsIgnoreCase("carrots"))
    {
    System.out.println("How many Carrots would you like? ");
    carrots = in.nextInt();
    grocery3amt = carrots * car;
    }

    // actual sum

    totalCost = grocery1amt + grocery2amt + grocery3amt;
    if ((totalCost >= 20.00)&&(totalCost < 40.00))
    {
    System.out.println("You have earned a 5% discount on your shopping");
    fiveDiscount = totalCost - (totalCost / .05);
    }





    }
    }
    Tagged:


Comments

  • Registered Users, Registered Users 2 Posts: 6,198 ✭✭✭Talisman


    Get rid of these curly braces:
    // second grocery
    [B]{[/B]
    System.out.println ("Please enter your second grocery: ");
    grocery2 = in.nextLine();
    [B]}[/B]
    


  • Registered Users Posts: 175 ✭✭TheFlidKid


    I was still having the problem before that, they weren't making a difference


  • Registered Users, Registered Users 2 Posts: 377 ✭✭CarefulNow


    Have a look at this question on Stackoverflow.


  • Registered Users, Registered Users 2 Posts: 8,324 ✭✭✭chrislad


    I remember having this issue when I started out last year. You need to create two scanner objects. You have a Scanner in = new Scanner(System.in), but you need to create another one, say, Scanner fruitQty = new Scanner(System.in);

    Use in for taking in the names of the fruits and fruitQty for taking in the qtys. I gave it a quick try and it worked for me. Also, look into methods! It'll save you a LOT of time! :)

    Edit: The StackOver flow answer is better though so you're better off reading that!


  • Registered Users Posts: 175 ✭✭TheFlidKid


    Thanks man I'll give that one a go now


  • Advertisement
  • Registered Users Posts: 175 ✭✭TheFlidKid


    I tried this but It came showed and error with the input.nextLine(); so i couldnt compile it


  • Registered Users, Registered Users 2 Posts: 2,089 ✭✭✭henryporter


    Should be the same scanner object in that case i.e.

    in.nextLine(); as opposed to input.nextLine();


  • Registered Users Posts: 175 ✭✭TheFlidKid


    I did what chrislad showed and it works fine


  • Registered Users, Registered Users 2 Posts: 2,089 ✭✭✭henryporter


    TheFlidKid wrote: »
    I did what chrislad showed and it works fine

    That's good - although as he also mentioned using methods will make things easier for you - easier code to read and work with, and better marks :-)


  • Registered Users Posts: 175 ✭✭TheFlidKid


    We actually haven't done methods yet and the project is due in a few days but I'll keep that in mind though :) btw how do you make a double show two decimal places?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,089 ✭✭✭henryporter


    TheFlidKid wrote: »
    We actually haven't done methods yet and the project is due in a few days but I'll keep that in mind though :) btw how do you make a double show two decimal places?

    :)

    printf ("%.2f", 3.14159);

    like println but using f for format


  • Registered Users Posts: 175 ✭✭TheFlidKid


    I got it. I just moved the decimal place of the grocery prices forward two


  • Registered Users Posts: 175 ✭✭TheFlidKid


    :)

    printf ("%.2f", 3.14159);

    like println but using f for format

    I had tried that but i had forgotten about the whole printf thing (still new at this) so i just changed the decimal place on the grocery prices to two decimal places forward


Advertisement