Help with java PLEASE PLEASE

Fenris

OSNN Senior Addict
Joined
16 Aug 2003
Messages
267
Im having problems getting my java program to work for my class. I keep getting this error when i try to do a subtraction and i cant figure out why it wont subtract anyway heres the error

operator - cannot be applied to java.lang.String,int
output = "" + (num1 * num2 * (yearString - num3));
^

for some reason it wont let me subtract :( if you need to see the whole program to figure out the problem ill paste it...its not that long only 20 lines....i think thats all ill need to make the program work. anyway its intro to java class so dont flame me if its a easy fix everyone has to start somewhere. :D

thanks
 
Share the solution with us, it may help someone else in the future.. :)
 
Alright well the problem was that the 'yearString' was never actually defined so i had to make a new line

double year = Double.parseDouble(yearString);

to show that yearString was defined as year and then the new equation is

output = "" + (num1 * num2 * (year - num3));

if that even makes sense to anyone....



anyway the program i had to make is to calculate the population on earth when someone puts in a year and it works yay :D now for the extra credit
 
It makes very much sense if you know how programming works.

yearString is, as it says, a string. It is treated as text. num3 is an integer, a number. If you think about it it makes sense that you can't subtract a number, say 5, from a piece of text. Even if that piece of text happens to be "2003", because the computer can't know this. It might just as well be "Hey there!" - 5, which is just silly. :)
 
since the variable was called yearString, it made sense that you could not sub-tract from it.
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back