I'm just a beginner please help!
BlueJ accepts my code - there are no compilation errors.
But when i want to run the program it doesnt do what i wanted it to. It allows the user to answer the questions about the product and price but the skips to the output after the question of the category appears not letting the user specify the category.
What am i doing wrong?

My coding:
The class:

public class Product
{
    private String name;
    private double price;
    private String category;

    public Product(String Name , double Price, String Category)
    {
        name = Name;
        price = Price;
        category = Category;
    }

    public String getDescription()
    {
        String description = "";
        if (category.equals("E"))
        description = "Essential";
        else if(category.equals("N"))
        description = "Nice to have";
        else if (category .equals("L"))
        description = "Luxury";
        else description = "Invalid";

        return description;
    }

    public String getName()
    {
        return name;
    }

    public double getPrice()
    {
        return price;
    }

    public String getCategory()
    {
        return category;
    }

}

The instance of the class:

import java.util.Scanner;
import javax.swing.JOptionPane;
public class testProduct
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        String control;
        control = JOptionPane.showInputDialog("Do you want to continue (Yes[Y] / No[N])? ");

        while (control.equals("Y"))
        {
            String name;
            double price;
            String category;

            System.out.print("\nEnter the name of the product: ");
            name = input.nextLine();
            System.out.print("Enter the price of the product: ");
            price = input.nextDouble();
            System.out.print("Enter the category of the product (E, N or L): ");
            category = input.nextLine();

            Product myProduct = new Product(name , price , category);

            System.out.println("\nName:\t\t" + myProduct.getName());
            System.out.println("Price:\t\t" + "R " + myProduct.getPrice());
            System.out.println("Category:\t" + myProduct.getDescription());

            control = JOptionPane.showInputDialog("Do you want to continue (Yes[Y] / No[N])? ");

        }
    }
}

My task was:
Assignment 2

Write a class called Product
- with a constructor receiving a name, price and category(E, N or L).
- Declare three private instance fields: name, price and category.
- Write a method getDescription to return a description of the category. The following criteria are used: E is Essential, N is Nice to have, L is Luxury and any other category is Invalid.
- Write get methods to return each of the instances fields.

In the testProduct class:
Write code to do the following. For each product:
- Ask the user whether he/she wants to continue (Yes or No) to control the loop.
- Ask the user to enter the name, price and category.
- Create a product object called myProduct. Initialize the object with the name, price and category.
- Call the getDescription method to get and display the description of the product.
- Use the get methods to display the information.

Thank YOU!!

Recommended Answers

All 3 Replies

I'm not 100% sure, but I have a theory. Are you pressing ENTER to finish entering the price? It seems like nextDouble isn't consuming an ENTER-press, so the following call to nextLine sees it, and assumes you entered nothing for the category. Try using nextLine to get the price and convert it to a double separately... if that works, then my theory is correct.

yes gusano79 is right.The error is because of line 21 price = input.nextDouble();.
Change this to price = Double.parseDouble(input.nextLine())
As it takes next line as category.To check this,just check the value of category and you can see that or start your code in debug mode and put breakpoint before line 21.

I changed my Line21 and it works!!
Thanks so much guys! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.