Help with java PLEASE PLEASE

Discussion in 'Web Design & Coding' started by Fenris, Sep 23, 2003.

  1. Fenris

    Fenris OSNN Senior Addict

    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
     
  2. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    Share the solution with us, it may help someone else in the future.. :)
     
  3. Fenris

    Fenris OSNN Senior Addict

    Messages:
    267
    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
     
  4. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    that makes sense.
     
  5. Zedric

    Zedric NTFS Guru Folding Team

    Messages:
    4,006
    Location:
    Sweden
    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. :)
     
  6. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    since the variable was called yearString, it made sense that you could not sub-tract from it.