Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Mind is going to burst with this code!!!

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Mind is going to burst with this code!!!

    Ive to write a software program that allows a user to order groceries?

    Ive to ask for the users details names, address etc.

    I have to ask the user to input their grocery item and then the quantity for 3 items only.

    and then add up the total cost and output the total cost to the screen.

    I have done the following but getting 3 errors I cant figure it out as Im only after starting to learn Java.


    import java.util.Scanner;

    class Grocery1
    {
    public static void main(String[]args)
    {
    Scanner in = new Scanner (System.in);
    // User details input needed. String variables.


    String name;
    String address;
    String creditCardNum;
    String expiryDate;
    String dob;
    String email;
    String email2;

    System.out.println("Please enter your Full Name ");{
    name = in.nextLine();}
    System.out.println("Please enter your Address ");{
    address = in.nextLine();}
    System.out.println("Please enter your Credit Card Number ");{
    creditCardNum = in.nextLine();}
    System.out.println("Please enter your Credit Card Expiry Date in 00/00 format ");{
    expiryDate = in.nextLine();}
    System.out.println("Please enter your Date of Birth in 00/00/00 format ");{
    dob = in.nextLine();}
    System.out.println("Please enter your email address ");{
    email = in.nextLine();}
    System.out.println("Please re enter your email address ");{
    email2 = in.nextLine();}

    //confirm the email

    if(email.equals(email2)){
    System.out.println(" Thank your email confirmation successful");}
    else
    {System.out.println(" Sorry you have entered the wrong email");}

    Scanner grocery1Item = new Scanner (System.in);


    String grocery1;
    int grocery1Amt;
    double grocery1Cost;


    System.out.print("Please enter the first grocery item " );
    grocery1 = grocery1Item.nextLine();

    System.out.print("Please enter the amount ");
    grocery1Amt = grocery1Item.nextInt();




    if(grocery1.equalsIgnoreCase("apples")){
    grocery1Cost = 0.30 * grocery1Amt;
    System.out.println("The total cost for Item 1 is " + grocery1Cost);}
    else if(grocery1.equalsIgnoreCase("oranges")){
    grocery1Cost = 0.45 * grocery1Amt;
    System.out.println("The total cost for Item 1 is " + grocery1Cost);}
    else if(grocery1.equalsIgnoreCase("strawberries")){
    grocery1Cost = 2.30 * grocery1Amt;
    System.out.println("The total cost for Item 1 is " + grocery1Cost);}
    else if(grocery1.equalsIgnoreCase("potatoes")){
    grocery1Cost = 3.25 * grocery1Amt;
    System.out.println("The total cost for Item 1 is " + grocery1Cost);}
    else if(grocery1.equalsIgnoreCase("turnips")){
    grocery1Cost = 0.70 * grocery1Amt;
    System.out.println("The total cost for Item 1 is " + grocery1Cost);}
    else if(grocery1.equalsIgnoreCase("carrots")){
    grocery1Cost = 1.25 * grocery1Amt;
    System.out.println("The total cost for Item 1 is " + grocery1Cost);}
    else{
    System.out.print(" You have entered an Invalid Item ");}


    Scanner grocery2Item = new Scanner (System.in);


    String grocery2;
    int grocery2Amt;
    double grocery2Cost;


    System.out.print("Please enter the second grocery item " );
    grocery2 = grocery2Item.nextLine();

    System.out.print("Please enter the amount ");
    grocery2Amt = grocery2Item.nextInt();




    if(grocery2.equalsIgnoreCase("apples")){
    grocery2Cost = 0.30 * grocery2Amt;
    System.out.println("The total cost for Item 2 is " + grocery2Cost);}
    else if(grocery2.equalsIgnoreCase("oranges")){
    grocery2Cost = 0.45 * grocery2Amt;
    System.out.println("The total cost for Item 2 is " + grocery2Cost);}
    else if(grocery2.equalsIgnoreCase("strawberries")){
    grocery2Cost = 2.30 * grocery2Amt;
    System.out.println("The total cost for Item 2 is " + grocery2Cost);}
    else if(grocery2.equalsIgnoreCase("potatoes")){
    grocery2Cost = 3.25 * grocery2Amt;
    System.out.println("The total cost for Item 2 is " + grocery2Cost);}
    else if(grocery2.equalsIgnoreCase("turnips")){
    grocery2Cost = 0.70 * grocery2Amt;
    System.out.println("The total cost for Item 2 is " + grocery2Cost);}
    else if(grocery2.equalsIgnoreCase("carrots")){
    grocery2Cost = 2.25 * grocery2Amt;
    System.out.println("The total cost for Item 2 is " + grocery2Cost);}
    else{
    System.out.print(" You have entered an Invalid Item ");}







    Scanner grocery3Item = new Scanner (System.in);


    String grocery3;
    int grocery3Amt;
    double grocery3Cost;



    System.out.print("Please enter the third grocery item ");
    grocery3 = grocery3Item.nextLine();

    System.out.print("Please enter the amount ");
    grocery3Amt = grocery3Item.nextInt();




    if(grocery3.equalsIgnoreCase("apples")){
    grocery3Cost = 0.30 * grocery3Amt;
    System.out.println("The total cost for Item 3 is " + grocery3Cost);}
    else if(grocery3.equalsIgnoreCase("oranges")){
    grocery3Cost = 0.45 * grocery3Amt;
    System.out.println("The total cost for Item 3 is " + grocery3Cost);}
    else if(grocery3.equalsIgnoreCase("strawberries")){
    grocery3Cost = 3.30 * grocery3Amt;
    System.out.println("The total cost for Item 3 is " + grocery3Cost);}
    else if(grocery3.equalsIgnoreCase("potatoes")){
    grocery3Cost = 3.35 * grocery3Amt;
    System.out.println("The total cost for Item 3 is " + grocery3Cost);}
    else if(grocery3.equalsIgnoreCase("turnips")){
    grocery3Cost = 0.70 * grocery3Amt;
    System.out.println("The total cost for Item 3 is " + grocery3Cost);}
    else if(grocery3.equalsIgnoreCase("carrots")){
    grocery3Cost = 3.35 * grocery3Amt;
    System.out.println("The total cost for Item 3 is " + grocery3Cost);}
    else{
    System.out.print(" You have entered an Invalid Item ");}
    {


    System.out.println("The total order cost is " + grocery1Cost + grocery2Cost + grocery3Cost);}




    }
    }

    Error: variable grocery1Cost might not have been initialized
    Error: variable grocery2Cost might not have been initialized
    Error: variable grocery3Cost might not have been initialized


    Please help!!


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Mind is going to burst with this code!!!

    Since those variables are initialized within if blocks, there's a chance that all if conditions will be false, and the variables will be used (in the System.out.println(...)) before they've been given a valid value. A solution is to give these variables a default value when you declare them, something that makes sense, perhaps 0.0. for instance.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Mind is going to burst with this code!!!

    From the compiler's point of view there is no guarantee that any of these three variables is ever assigned a value.

    To get the program to compile so that you can start debugging, just initialize each of them to 0.0 when it is declared.


    Cheers!

    Z

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Mind is going to burst with this code!!!

    had same problem other day shoulda known silly me!! thank you so much i feel like crying with relief!!!

Similar Threads

  1. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  2. Replies: 7
    Last Post: August 1st, 2012, 12:37 PM
  3. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 07:36 AM
  4. The Concept of Entities -- Can't wrap my mind around it.
    By RaustBD in forum Java Theory & Questions
    Replies: 2
    Last Post: November 21st, 2011, 07:42 PM
  5. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 08:30 PM