Question About Variables

Zachary Boddy

Staff member
Aug 3, 2014
2,410
15
38
www.windowscentral.com
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.
 

Zachary Boddy

Staff member
Aug 3, 2014
2,410
15
38
www.windowscentral.com
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.
 

Zachary Boddy

Staff member
Aug 3, 2014
2,410
15
38
www.windowscentral.com
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:

Nicholas Ek

New member
Jun 30, 2015
177
0
0
Visit site
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); }
 

Zachary Boddy

Staff member
Aug 3, 2014
2,410
15
38
www.windowscentral.com
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.
 

Nicholas Ek

New member
Jun 30, 2015
177
0
0
Visit site
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++.
 

Zachary Boddy

Staff member
Aug 3, 2014
2,410
15
38
www.windowscentral.com
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:

Nicholas Ek

New member
Jun 30, 2015
177
0
0
Visit site
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.
 

Zachary Boddy

Staff member
Aug 3, 2014
2,410
15
38
www.windowscentral.com
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.
 

Zachary Boddy

Staff member
Aug 3, 2014
2,410
15
38
www.windowscentral.com
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.
 

Zachary Boddy

Staff member
Aug 3, 2014
2,410
15
38
www.windowscentral.com
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.
 

Members online

Forum statistics

Threads
326,655
Messages
2,248,712
Members
428,532
Latest member
daniel231