12 Replies - 3988 Views - Last Post: 13 February 2012 - 07:04 AM Rate Topic: -----

#1 asylum3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-February 12

the operator - is undefined for the argument type(s) String, int

Posted 13 February 2012 - 06:31 AM

import java.util.Scanner;

public class SoftwareSales {

    /**
	 * @param args
	 */
	public static void main(String[] args) {

		//constructors
		int quantity;             //number bought
		int price;                //price of software
		int discountRate;         //discount from sales
		int totalDiscount;        //discount for total price
		int priceBefore;          //price before discount		
		
//create scanner for keyboard
Scanner keyboard = new Scanner(System.in);

//put in price
System.out.println("Enter price of software");
price = keyboard.nextInt();

//get the number of sales
System.out.print("Enter quantity sold");
quantity = keyboard.nextInt();



//determine the discount 
if (quantity >= 10 && quantity <= 19)
	System.out.println("Your discount is 20%");
else if (quantity >= 20 && quantity <= 49)
	System.out.println("Your discount is 30%");
else if (quantity >= 50 && quantity <= 99)
	System.out.println("Your discount is 40%");
else if (quantity >= 100)
	System.out.println("Your discont is 50%");


//total before discount
System.out.println("Your price before the discount is $" + price * quantity);

//get discount rate
System.out.print("Enter in discount rate");
discountRate = keyboard.nextInt();

//total discount
System.out.println("total discount is $" + price * discountRate);

//total price of purchases
System.out.print("Enter the total price of purchases before discount");
priceBefore = keyboard.nextInt();

//money taken off
System.out.print("Enter in total discount");
totalDiscount = keyboard.nextInt();

//total price
System.out.println("total price is " + priceBefore - totalDiscount);

	}

}



I am new to java just started the online class a few weeks ago, I keep getting an error that says "The operator - is undefined for the argument type(s) String, int"
could you please let me know what I am doing wrong? thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: the operator - is undefined for the argument type(s) String, int

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: the operator - is undefined for the argument type(s) String, int

Posted 13 February 2012 - 06:33 AM

Even though both priceBefore and totalDiscount are ints, the + operator concatenates the String and priceBefore first, then tries to apply the - operator to the String and totalDiscount. You cannot subtract a String and int. If you surround the expression priceBefore - totalDiscount in parentheses, the expression will be evaluated before the concatenation takes place.
Was This Post Helpful? 1
  • +
  • -

#3 asylum3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-February 12

Re: the operator - is undefined for the argument type(s) String, int

Posted 13 February 2012 - 06:39 AM

Thx! that fixed it. also when I run it and I get to the discount rate part I tried .2 and also 20% and neither one works, is there something else wrong with that part of the code?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: the operator - is undefined for the argument type(s) String, int

Posted 13 February 2012 - 06:41 AM

You're going to have to be more specific about how it's not working.
Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: the operator - is undefined for the argument type(s) String, int

Posted 13 February 2012 - 06:42 AM

The discountRate variable will need to be a double since you are entering in .2 which is a double not an int.

Also,
//total price of purchases
System.out.print("Enter the total price of purchases before discount");
priceBefore = keyboard.nextInt();
	 
//money taken off
System.out.print("Enter in total discount");	totalDiscount = keyboard.nextInt();


Are you required to have them enter those again? If I am not mistaken you just did the calculation for both above so I do not see why you need them to re enter it.

This post has been edited by Fuzzyness: 13 February 2012 - 06:44 AM

Was This Post Helpful? 1
  • +
  • -

#6 asylum3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-February 12

Re: the operator - is undefined for the argument type(s) String, int

Posted 13 February 2012 - 06:47 AM

thx Fuzzyness, that worked, but now everytime I put in .2 it automatically says the interest is $19.8 even though the prices have varied by several thousand dollars. sorry if im asking too many questions.
Was This Post Helpful? 0
  • +
  • -

#7 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: the operator - is undefined for the argument type(s) String, int

Posted 13 February 2012 - 06:50 AM

Re-post your current code you have since you made changes and then the numbers that you are entering so we are on the same page.
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: the operator - is undefined for the argument type(s) String, int

Posted 13 February 2012 - 06:52 AM

If you do any changes to your program first post updated code before asking another question. This will give us an idea of what you have updated. Also a sample output may be useful
Was This Post Helpful? 1
  • +
  • -

#9 asylum3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-February 12

Re: the operator - is undefined for the argument type(s) String, int

Posted 13 February 2012 - 06:53 AM


import java.util.Scanner;

public class SoftwareSales {

    /**
	 * @param args
	 */
	public static void main(String[] args) {

		//constructors
		int quantity;             //number bought
		int price;                //price of software
		double discountRate;      //discount from sales
		int totalDiscount;        //discount for total price
		int priceBefore;          //price before discount		
		
//create scanner for keyboard
Scanner keyboard = new Scanner(System.in);

//put in price
System.out.println("Enter price of software");
price = keyboard.nextInt();

//get the number of sales
System.out.print("Enter quantity sold");
quantity = keyboard.nextInt();



//determine the discount 
if (quantity >= 10 && quantity <= 19)
	System.out.println("Your discount is 20%");
else if (quantity >= 20 && quantity <= 49)
	System.out.println("Your discount is 30%");
else if (quantity >= 50 && quantity <= 99)
	System.out.println("Your discount is 40%");
else if (quantity >= 100)
	System.out.println("Your discont is 50%");


//total before discount
System.out.println("Your price before the discount is $" + price * quantity);

//get discount rate
System.out.print("Enter in discount rate");
discountRate = keyboard.nextDouble();

//total discount
System.out.println("total discount is $" + price * discountRate);

//total price of purchases
System.out.print("Enter the total price of purchases before discount");
priceBefore = keyboard.nextInt();

//money taken off
System.out.print("Enter in total discount");
totalDiscount = keyboard.nextInt();

//total price
System.out.println("total price is " + (priceBefore - totalDiscount));

	}

}




example of my program:

Enter price of software
99
Enter quantity sold 45
Your discount is 30%
Your price before the discount is $4455
Enter in discount rate .2
total discount is $19.8
Enter the total price of purchases before discount
Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: the operator - is undefined for the argument type(s) String, int

Posted 13 February 2012 - 06:57 AM

System.out.println("total discount is $" + price * discountRate);
You are doing the discount on only the price of 1 item. You need to change that price to be the total price of them all and not just one individual item.

20% of 99 is $19.8 So it is doing what you tell it to right now

This post has been edited by Fuzzyness: 13 February 2012 - 06:58 AM

Was This Post Helpful? 1
  • +
  • -

#11 asylum3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-February 12

Re: the operator - is undefined for the argument type(s) String, int

Posted 13 February 2012 - 06:59 AM

Thx for all the help :)
Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: the operator - is undefined for the argument type(s) String, int

Posted 13 February 2012 - 07:01 AM

What is the problem there? You want the price to be 4455 instead of 99? Because you have assign nothing to update the price value, you are calculating and display it.
EDIT: very slow!! Glad you get it..

This post has been edited by smohd: 13 February 2012 - 07:04 AM

Was This Post Helpful? 0
  • +
  • -

#13 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: the operator - is undefined for the argument type(s) String, int

Posted 13 February 2012 - 07:04 AM

Few more things...
System.out.println("total discount is $" + price * discountRate);
System.out.println("Your price before the discount is $" + price * quantity);

That is your original error, you only changed 1 of the 3 statements... you need to put the ( ) around those 2 as well.

Also, I do not see you use this variable anywhere int priceBefore; I am assuming that is for the price*quantity portion of it. I would suggest that before the println for the price before quantity you assign the variable a value and use the variable in that statement and the one for the final discount.

Ex.
int priceBefore = price * quantity;
System.out.println("message for price before" + priceBefore);

System.out.println("Message for price discount" + (pricebefore*discountRate));


Something a long those lines.

Edit - I see you don't assign values to many of those variables you created in the beginning, I would suggest learning to use variables more often so you don't have to redo calculations. However, if you don't want to use them get rid of them so you are not wasting them by not using them. :)

This post has been edited by Fuzzyness: 13 February 2012 - 07:06 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1