7 Replies - 619 Views - Last Post: 10 September 2011 - 12:06 PM Rate Topic: -----

#1 mnosek3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 10-September 11

Undefined operator-Newbie Question

Posted 10 September 2011 - 08:50 AM

package assignment1;

public class Two {

public static void main(String[] args) {

		int x=10;
		
		System.out.println("Test1 "+x*3*2.0);
		System.out.println("Test2 "+x*3+2.0);
		System.out.println("Test3"+x*3-2.0);
		
				
	}

}



This is my first Java homework assignment and I am not sure why I keep getting this error. When I run the first two lines test1 and test2 it works fine but then when I add test 3 this error come up.

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The operator - is undefined for the argument type(s) String, double

at assignment1.Two.main(Two.java:12)


Could someone please explain this to me? Thanks for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Undefined operator-Newbie Question

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

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

Re: Undefined operator-Newbie Question

Posted 10 September 2011 - 08:58 AM

If you put the math in parenthesis it will compile.
System.out.println("Test3" + (x*3-2.0));
Was This Post Helpful? 1
  • +
  • -

#3 mnosek3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 10-September 11

Re: Undefined operator-Newbie Question

Posted 10 September 2011 - 09:06 AM

Thanks! But why do the first two statements work? I am just trying to understand....
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


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

Re: Undefined operator-Newbie Question

Posted 10 September 2011 - 09:06 AM

You need to know about operation precisions, you have a +, * and - sign, so what will happen, the program will start x*3, then dealing from left(since +,- have same level), so concatenating "Test3" + 30 now the data is changed to string because one of the operand is string, and the remaining data will all be treated as string concatenation and not adding, so since string does not support subtraction, that error will raise try to put:
System.out.println("Test3"+(x*3-2.0));

Was This Post Helpful? 3
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


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

Re: Undefined operator-Newbie Question

Posted 10 September 2011 - 09:12 AM

View Postmnosek3, on 10 September 2011 - 09:51 PM, said:

Thanks! But why do the first two statements work? I am just trying to understand....

Works but with wrong answers like

Quote

Test1 60.0 //this is correct because the math was all *, and it has high precision before +, so it is done first
Test2 302.0 //this is wrong because x*3+2.0 = 32.0 but what it does it x*3 = 30, that concatenate Test1 + 30 = Test1 30,
// then concatenate Test1 30 + 2.0 = Test1 302.0

Was This Post Helpful? 0
  • +
  • -

#6 mnosek3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 10-September 11

Re: Undefined operator-Newbie Question

Posted 10 September 2011 - 09:18 AM

Thanks for the explanation smohd! I am glad I found this site. It will be helpful through out the semester.
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


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

Re: Undefined operator-Newbie Question

Posted 10 September 2011 - 09:20 AM

Glad we could help, you are welcomed :)
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7964
  • View blog
  • Posts: 13,588
  • Joined: 19-March 11

Re: Undefined operator-Newbie Question

Posted 10 September 2011 - 12:06 PM

View Postsmohd, on 10 September 2011 - 11:06 AM, said:

You need to know about operation precisions, you have a +, * and - sign, so what will happen, the program will start x*3, then dealing from left(since +,- have same level), so concatenating "Test3" + 30 now the data is changed to string because one of the operand is string, and the remaining data will all be treated as string concatenation and not adding, so since string does not support subtraction, that error will raise try to put:
System.out.println("Test3"+(x*3-2.0));


"Precedence" is the word you're looking for here. Order of operations. "Precision" is another matter: how close the result will be to the actual result, usually measured in decimal places. (pi = 3.14 is correct to three places)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1