Results 1 to 4 of 4
  1. #1
    iminhell is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default using decimal numbers

    First off, I did search a bit. But being new and not really understanding much I'm not quite sure how to word things to get the answer I want. What I was able to find confused me more. But I did find a trend in answers, to which I'm sure someone will elude to.


    Reading here and I don't quite understand it. (think it has something to do with not being able to see the result or what's being input)

    Right now I've got this:
    Java Code:
    int p = Integer.parseInt(pres.getText());
    I think it should look something like this:
    Java Code:
    int t = NumberFormat.getIntegerInstance(temp.getText());
    But that gives me an error that says "cannot find symbol" and the 'temp.getText());' is underlined (I know that means that's the trouble spot).

    So how should I fix this?

    I have "java.text.NumberFormat" imported. And I think (from what I've found) there has to be a format and wording similar to the link I've posted ... "try" and "catch",, but where do user input numbers fit into those lines?


    Not sure if it matters but I'm using NetBeans 6.9.1 and this is a jFrame application with tabs; very similar to what this tutorial taught me to do. Which I've noticed has the same problem. You can't input 30.5 hours worked or any decimal number. That is what i wish to fix and can not find how.
    Also I'm am learning on my own from tutorials and searching. Never done any programming before. So be gentle please.


    --John
    Last edited by iminhell; 10-31-2010 at 03:43 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by iminhell View Post
    First off, I did search a bit. But being new and not really understanding much I'm not quite sure how to word things to get the answer I want. What I was able to find confused me more. But I did find a trend in answers, to which I'm sure someone will elude to.
    You may need to tell us what you are trying to do first as I don't see this fully explained anywhere in your post.

    Right now I've got this:
    Java Code:
    int p = Integer.parseInt(pres.getText());
    What is wrong with this? How does it not fulfill whatever criteria you have (again we need to know the criteria to be able to help you)?


    I think it should look something like this:
    Java Code:
    int t = NumberFormat.getIntegerInstance(temp.getText());
    But that gives me an error that says "cannot find symbol" and the 'temp.getText());' is underlined (I know that means that's the trouble spot).
    The error is well deserved since if you read the API for NumberFormat you'll see that getIntegerInstance returns a NumberFormat object, not an int. Again, what are you trying to accomplish (Sorry if I'm sounding like a broken record)?

  3. #3
    iminhell is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    lol
    Was hoping someone would look at the tutorial link. It has the same loss of function I was dealing with.

    After hours of searching and the loss of a rather large portion of my already thinning hair, I found the answer. It is located in this tutorial.

    So I changed my first code to the now working code of:
    Java Code:
    float t, p, pci, fps;
    
    // Gets temperature from the text field, and then parses it to type float.
    t = Float.parseFloat(temp.getText());
    p = Float.parseFloat(pres.getText());
        
    //Simple statement for calculations
    
    pci =   (float) (1.325 * (2.036254 * p / (459.67 + t)));
    fps =   (float) Math.sqrt(1.4*1716*(t+459.67));
    
    //Writes the total
    jLabel5.setText(String.valueOf(pci));
    jLabel7.setText(String.valueOf(fps));
    Happens to be the only help I've read where the float function was mentioned. Not sure if it's correct, but it works and I'm going with it. Leave any problems for a battle another time.
    Like I'd mentioned, I was quite sure I was searching wrong. Just happened on this one by chance.


    *I don't see any option to close or "label as solved". Not sure if you guys just let them drift away or what, but I think I'm good for now. Thanks for the time.
    Last edited by iminhell; 10-31-2010 at 04:44 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Congratulations on solving it and especially on solving it yourself, as you will definitely learn from this experience.

    To mark this thread as "solved" please click on the "Thread Tools" link near the top of this thread.

    One last thing, consider using double rather than float since it is much more accurate with little extra cost.

    edit: also, thanks for posting a solution, so now you can share the knowledge you've gained with others! :)
    Last edited by Fubarable; 10-31-2010 at 04:50 AM.

Similar Threads

  1. converting a decimal to an int
    By shuks in forum New To Java
    Replies: 9
    Last Post: 10-12-2009, 10:41 AM
  2. Replies: 4
    Last Post: 01-16-2009, 07:17 PM
  3. Truncating decimal numbers in the output
    By gbade in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 07:25 PM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM
  5. numbers with two decimal places
    By little_polarbear in forum New To Java
    Replies: 8
    Last Post: 08-28-2008, 12:04 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
  •