Results 1 to 4 of 4
  1. #1
    tpyq is offline Member
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

    Default [SOLVED] operator / cannot to applied to java.lang.string,int

    I have a label call scoreLabel and it shows the score the user will get. I want to convert the score to 100% so i try this

    int sco = ((scoreLabel.getText()/16)*100); (16 is the total score)

    But i get the error message saying operator / cannot to applied to java.lang.string,int

    Can anyone help me with this?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default

    Read the API for JLabel and you'll see that getText() returns a String. Do you seriously expect to be able to divide a String by an int?

    Keep track of the score in a variable of numeric type and use that for the calculation. And be aware of integer division so you use suitable variable types.

    db

  3. #3
    Ch4os is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default

    Try
    int sco = (Integer.valueOf(scoreLabel.getText())/16)*100;

    Integer.valueOf(String)
    Converts a string to a int

  4. #4
    tpyq is offline Member
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 02:12 AM
  2. Replies: 4
    Last Post: 11-27-2008, 06:09 AM
  3. String Object equality with == operator
    By kumar080780 in forum New To Java
    Replies: 4
    Last Post: 10-29-2008, 06:57 AM
  4. Replies: 2
    Last Post: 09-18-2008, 07:30 AM
  5. Error: cannot be applied to (java.lang.String)
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •