tags:

views:

301

answers:

9

I have this but there is an error, I don't get it :(. I'm relatively new to Java.

package hw;
import java.util.Scanner;

public class Problem1
{
    public static void main (String [] args)
    {
        int cost; int number; double cost_unit; double total;

        Scanner entrada = new Scanner(System.in);            

        System.out.println("Please enter the cost of the product.");
        cost = entrada.nextInt();

        while (cost>0){

            System.out.println("Please enter the amount of units to be sold");
            number = entrada.nextInt();

            if (number>0);

            cost_unit = cost * 1.4;
            total = cost_unit*number;

            System.out.printf("Cost per unit will be $ %d\n",cost_unit);
            System.out.printf("Cost per unit will be $ %d\n",total);                                
        }
    }                
}

// I just want the user to enter the cost for a product, give a number of units to be ordered, and I want the program find out the final price of the product with a 40% profit.

+1  A: 

Some hints:

while (cost>0)

Do you really want a while-loop here? You seem to be checking if the cost is positive.

if (number>0);

This if statement doesn't do anything useful.

Zach Scrivena
I suggest the post author use an IDE like NetBeans... it would've underlined `if (number>0)` and given a warning.
cdmckay
+1  A: 

It would seem the "if (number>0);" statement wouldn't help much.

Try changing it to:

if (number>0) {
  cost_unit = cost * 1.4;
  total = cost_unit*number;

  System.out.printf("Cost per unit will be $ %d\n",cost_unit);
  System.out.printf("Cost per unit will be $ %d\n",total);
}

EDIT: Also, it seems there is an infinite loop if the (first) cost specified is > 0, since you aint changing it (cost) in the loop.

baretta
+1  A: 

Without looking too deep at the code, one thing that catch my attention is:

if (number>0);

cost_unit = cost * 1.4;
total = cost_unit*number;

Probably you would like this:

if (number>0) {
    cost_unit = cost * 1.4;
    total = cost_unit*number;
}

BTW, I've seen this before..... but I'm not quite sure where...

OscarRyz
thanks man! but now that I am running the program it seems my variables are incorrectly declared, any tips on that?
El Toro
+1  A: 

change

    if (number>0);

    cost_unit = cost * 1.4;
    total = cost_unit*number;

to

        if (number>0) {
        cost_unit = cost * 1.4;
        total = cost_unit*number;
        }
Tomas
+2  A: 

In printf, %d is for a signed decimal integer, whereas cost_unit and total are doubles. You should use %f instead.

System.out.printf("Cost per unit will be $ %f\n",cost_unit);
System.out.printf("Cost per unit will be $ %f\n",total);
Rich Adams
+1  A: 

If you study the documentation for the format syntax that you are using when printing your result, you will realize you are using integer formatting (%d) where you want to format as a floating point number (%f).

Fabian Steeg
+1  A: 

The last two lines should be:

System.out.printf("Cost per unit will be $ %f\n", cost_unit);
System.out.printf("Total cost will be $ %f\n", total);
Skip Head
+1  A: 

Here, try this:

I think it pretty much does what you need.

package hw;

import java.util.Scanner;


public class Problem1 {
    public static void main (String [] args) {
        int cost;
        int number; 
        double cost_unit = 0; 
        double total = 0;

        Scanner entrada = new Scanner(System.in);

        System.out.println("Please enter the cost of the product.");
        cost = entrada.nextInt();
        System.out.println("Please enter the amount of units to be sold");
        number = entrada.nextInt();

        cost_unit = cost * 1.4;
        if (number>0) {
            total = cost_unit*number;
        }

        System.out.printf("Cost per unit will be $ %f\n",cost_unit);
        System.out.printf("Total cost will be $ %f\n",total);
    }
}

Try it out and see if it works. By any chance are you at the ITAM?

EDIT

About the loop your original was correct. Just have to surround the code you want to repeat, and add a condition to exit.

Something like this after creating the scanner ( pretty much the way you did in the first attempt ) :

 while( cost > 0 ) { 
     System.out.println("Please enter the cost of the product ( 0 to exit the progam");
     cost = entrada.nextInt();
     .........
     .........
     .........
     System.out.printf("Total cost will be $ %f\n",total)

 }

That will repeat the code between braces while cost is greater than 0.

Of course you should change the initial value of cost, otherwise it won't enter in the lopp the first time and perhaps you should clean up the values before each iteration.

OscarRyz
It works ! That's a negative, I study in Honduras.In btw.. How can I make the program repeat itself, I was thinking of using a loop but I don't have much experience with it :)
El Toro
Probably your teacher pull the exercise from the same place.
OscarRyz
+2  A: 

Try and actually describe the problem:

Did javac not compile it? That's a syntax problem.

Did the program not behave as you intended it to be? What parts is not behaving? That's a logic problem.

Without adequate description of the problem, how could you expect that you, or others, could locate the errors easily? You're going to need this skill as you go on learning more about programming.

Back to finding problems in your code:

  • cost of the individual items are integers. Doesn't make sense if I want to sell gum that costs .75 cents
  • the while loop would loop infinitely if you enter anything but 0 for the cost
  • if condition does nothing... i.e. it makes no difference whether you've entered 0 or less units or more than 0 units
  • cost_unit = cost * 14 you probably want to name cost_unit to something like retail_unit - because that's the retail price. cost * profit margin = retail price, no?
Calyth