1. Zachary Boddy's Avatar
    Hey guys sorry for the slightly vague title. I need help with something. I'm learning basic Java and I've been rolling a personal quandary through my head all day. In a situation where:
    double x = 10;
    How do I display x as an integer? I understand I could simply say:
    System.out.println(+ (int)x);
    But this is only temporary and only covers a single value of x. If the value of x is changed to 10.5, for example, than x will be displayed as 10. In situations where double x is in fact a decimal, I'd like it to display as a decimal. In situations where x is an integer (whole number) or perhaps where the value of x minus (int)x is less than or equal to say .01, then I'd like x to display as an integer. I've been playing with if, then statements, but nothing I can come up with, with my very limited knowledge, seems to work. I figured out a way where in situations where x - (int)x (this has been set to int y) is less than .01, x is displayed as a double, yet a whole number. To explain, if x = 1.001, then x is displayed as 1.0. However this doesn't really help me. I would really appreciate help for this.
    Thank you.
    10-20-2015 05:44 PM
  2. Nicholas Ek's Avatar
    My suggestion is to use a new variable: y = (int) x
    Then if x - y > 0.01 OR x - y < -0.01 print x, else print y.
    10-20-2015 05:54 PM
  3. Zachary Boddy's Avatar
    My suggestion is to use a new variable: y = (int) x
    Then if x - y > 0.01 OR x - y < -0.01 print x, else print y.
    I tried that and it seemed to display x as a whole number, but not as an integer when it met those conditions. Meaning, if x = 1.001, then x was printed as 1.0, instead of 1.001. However, what I wish is for x to be displayed as simply 1, even though x is declared as a double and not an integer. I figured the reason it didn't work the way you suggested was because type casting (saying (int)x) is temporary and only applies to that specific line. So attempting to print (int)x actually just prints x, as (int)x only applies to the line it was declared on.
    10-20-2015 06:36 PM
  4. Zachary Boddy's Avatar
    public class VariablesExp2
    {
    public static void main(String [] args)
    {
    double x = 1;
    int y = (int)x;

    if(x - y < .01)
    x = y;

    if(x - y > -.01)
    x = y;

    System.out.println(+ x);
    }
    }

    This was my latest attempt at figuring out a way to do this. The result was 1.0. I've tried multiple ways (even "outsourcing" the variables to a separate method) but I always get the same result. Either it's something small and I'll feel silly for it or it's something major and as of now, beyond my knowledge and skills.
    If the formatting is weird on the above programming that's Windows Central not how I actually space it. There's indents there but Windows Central doesn't seem to like indents so.
    Last edited by Zachary Boddy; 10-20-2015 at 06:42 PM. Reason: Just editing. Juuuust editing.
    10-20-2015 06:40 PM
  5. Nicholas Ek's Avatar
    Your code is a bit wrong.
    Try replacing the rest of the body with something like this:

    if((x - y < -0.01) || (x - y > 0.01)) {
    System.out.println(x); }
    else {
    System.out.println(y); }
    10-20-2015 06:47 PM
  6. Zachary Boddy's Avatar
    Your code is a bit wrong.
    Try replacing the rest of the body with something like this:

    if((x - y < -0.01) || (x - y > 0.01)) {
    System.out.println(x); }
    else {
    System.out.println(y); }
    That worked! Thank you very much. Now I have a few more questions:
    How would I rework it to work only if double x equals a whole number? I attempted:
    if((x - y == 0)
    {
    System.out.println(+ x);
    }
    else
    {
    System.out.println(+ y);
    }
    But this didn't seem to work as it printed 1.0. Also, is there a way to have this apply to the entire program, rather than what's contained within the if, else statement? For example, writing System.out.println(+ x); directly below this would result in 1.0 being printed.
    10-20-2015 07:04 PM
  7. Nicholas Ek's Avatar
    if (x - y) {
    System.out.println(x); }
    else {
    System.out.println(y); }

    x is a variable of type double and as such, all numbers - including integers - will be saved as doubles in it and printed as doubles when called through x. To make the above-coded feature available through-out the program, you need to make it into a separate function. For printing, that is easily doable. For other things, it's more complicated (since a function can only have one return type).

    P.S.: I'm not a Java developer, I just know all this through C++.
    10-20-2015 07:19 PM
  8. Zachary Boddy's Avatar
    if (x - y) {
    System.out.println(x); }
    else {
    System.out.println(y); }

    x is a variable of type double and as such, all numbers - including integers - will be saved as doubles in it and printed as doubles when called through x. To make the above-coded feature available through-out the program, you need to make it into a separate function. For printing, that is easily doable. For other things, it's more complicated (since a function can only have one return type).

    P.S.: I'm not a Java developer, I just know all this through C++.
    Well I understand that many programming languages are very similar and only the syntax is different.
    How would I go about making it a separate function? I'm trying to think of a way to word an if, else statement to apply to every print function rather than the one print function associated with the if, else statement that solved part of my problem. The if, else statement you gave me applies to only a single calculation of variable x and not to all print functions regarding variable x. I would understand if I'd have to apply an altered if, else statement for every print function including variable x manually, but I feel there should be a way to do it (through an if, else statement or something similar) to apply to an entire program or at least an entire method rather than a single function.

    Edit: I figured out my issue with if(x - y == 0) was. I forgot to switch the else statements, so when the condition was met the program was printing x instead of y. That was my mistake.
    Last edited by Zachary Boddy; 10-20-2015 at 08:04 PM. Reason: I added an edit.
    10-20-2015 07:59 PM
  9. Nicholas Ek's Avatar
    Your function would look like this:

    void varprint(double x) {
    int y = (int) x;
    if (x - y) {
    System.out.println(x); }
    else {
    System.out.println(y); }
    }


    and then in the program you'd call it like this:

    varprint(age);
    varprint(12.44);
    varprint(8);

    I think you get my point now.
    10-20-2015 08:22 PM
  10. Zachary Boddy's Avatar
    Your function would look like this:

    void varprint(double x) {
    int y = (int) x;
    if (x - y) {
    System.out.println(x); }
    else {
    System.out.println(y); }
    }


    and then in the program you'd call it like this:

    varprint(age);
    varprint(12.44);
    varprint(8);

    I think you get my point now.
    Thank you I'll have to try it tomorrow. I'll get back to you on it.
    10-20-2015 10:21 PM
  11. Zachary Boddy's Avatar
    public class VariablesExp2Mod
    {
    public static void main(String [] args)
    {
    double x = 1;
    int y = (int)x;

    void varprint(double x)
    {
    int y = (int)x;
    if (x - y)
    {
    System.out.println(x);
    }
    else
    {
    System.out.println(y);
    }
    }

    // if(x - y == 0)
    // {
    // System.out.println(y);
    // }
    // else
    // {
    // System.out.println(x);
    // }
    }
    }
    I know I did something wrong and it's probably very silly of me but I mean I've been learning Java for exactly one week.
    10-21-2015 06:43 AM
  12. Nicholas Ek's Avatar
    varprint should be a separate function, i.e. outside the scope of main.
    10-21-2015 07:43 AM
  13. Zachary Boddy's Avatar
    varprint should be a separate function, i.e. outside the scope of main.
    Is it not within a method? Or is it its own separate method? Meaning,
    class
    main
    varprint method
    10-21-2015 03:40 PM
  14. Nicholas Ek's Avatar
    Yes, a separate method.
    10-22-2015 02:04 AM
  15. Zachary Boddy's Avatar
    Yes, a separate method.
    Thank you.
    10-22-2015 04:23 PM
  16. Zachary Boddy's Avatar
    I'm happy with what I have now. I've taken what I've learned from you and used it to develop the most complex program I've coded so far and I'm very proud of it. I mean, it's only 60 lines long but I'm still very happy with it.
    10-22-2015 04:27 PM
  17. Nicholas Ek's Avatar
    I'm happy with what I have now. I've taken what I've learned from you and used it to develop the most complex program I've coded so far and I'm very proud of it. I mean, it's only 60 lines long but I'm still very happy with it.
    I'm glad. Good luck with further development! :)
    10-22-2015 05:48 PM
  18. Zachary Boddy's Avatar
    I'm glad. Good luck with further development! :)
    Thank you.
    10-22-2015 08:45 PM

Similar Threads

  1. About Minecraft..
    By Slovenix in forum Phone & Tablet Gaming
    Replies: 3
    Last Post: 11-10-2015, 11:43 AM
  2. About sending SMS messages from Cortana in Windows 10 (desktop)
    By alb3530 in forum Windows 10 Insider Preview
    Replies: 3
    Last Post: 10-24-2015, 07:55 AM
  3. Replies: 1
    Last Post: 10-19-2015, 07:06 AM
  4. What do you like about it so far? I found so many.
    By excalibur1814 in forum Microsoft Surface Pro 4
    Replies: 1
    Last Post: 10-19-2015, 06:34 AM
  5. a question to people who tested windows 10
    By NSPerl in forum Windows 10 Mobile Insider Preview
    Replies: 2
    Last Post: 10-17-2015, 08:04 AM
LINK TO POST COPIED TO CLIPBOARD