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 2 of 2

Thread: How do call an object in an if else if statement.

  1. #1
    Junior Member
    Join Date
    Mar 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do call an object in an if else if statement.

    I am working on an inventory program where I have the user input some information about a product they would like to buy. I then use an if else if statement to create the object depending what the user typed in. However at the end of the program I want to recall that object, but don't know how to. The program stores it, I can't assign it as a variable so what do I do?

    System.out.println("Welcome to our hardware shop. We have three items in stock: \n" + "Wrench CMMT12001\n"
    + "Power Drill 12v\n"
    + "Shovel D-Handle\n"
    + "Which product would you like to buy?");

    product = keyboard.nextLine();

    System.out.println("How many items of this product would you like?");

    items = keyboard.nextInt();

    System.out.println("What is the date (00/00/0000) ");

    date = keyboard.nextLine();

    int dayOfYear = dayOfYear(date);


    if (product.equals("Wrench CMMT12001")) {
    Products wrench = new Products(productXID, product, productXPrice, "X", items, dayOfYear);

    }

    else if (product.equals("Power Drill 12v")) {
    Products drill = new Products(productYID, product, productYPrice, "Y", items, dayOfYear);
    }

    else if (product.equals("Shovel D-Handle")) {
    Products shovel = new Products(productZID, product, productZPrice, "Z", items, dayOfYear);
    }

    else
    System.out.println("You did not enter a product.");

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do call an object in an if else if statement.

    at the end of the program I want to recall that object
    I assume you are asking how to have the variable that refers to the object that is created inside of the if/else if statements be in scope outside of the if/else if statements. The way to do that is to declare the variable and assign it a null value before the if/else if statements. That way it will be in scope afterwards.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to call a self created method from an object stored in an ArrayList ?
    By Bubba_56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 21st, 2018, 11:11 AM
  2. Replies: 4
    Last Post: September 9th, 2014, 04:37 PM
  3. how to call an object with a string or variable?
    By Zoli in forum Java Theory & Questions
    Replies: 5
    Last Post: September 14th, 2013, 05:32 PM
  4. Call a method in all instances of an object
    By MaximusPrime in forum Java Theory & Questions
    Replies: 2
    Last Post: March 14th, 2012, 10:39 PM
  5. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM