Jump to content

Java: How to parse Double into Int?

Hip
Go to solution Solved by Adorable Cat,

Couldn't you just cast the double to an int, like

int convertedFromDouble = (int)inputDouble;

 

Hey guys,

 

I have written a code where I put in a double and then convert the double into a Integer. But this somehow does not work.

Can you tell me how to solve this?

 

import javax.swing.*;

public class Test
{
	public static void main(String[] args)
    {
    	double inputDouble;
        int convertedFromDouble;
        
        inputDouble = Double.parseDouble(JOptionPane.showInputDialog(null, "Put in a Double"));
        
        convertedFromDouble = Integer.parseInt(inputDouble);
        
        JOptionPane.showMessageDialog(null, "Your Double is "+inputDouble);
        JOptionPane.showMessageDialog(null, "Your Int ist "+convertedFromDouble);
        System.exit(0);
    }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Couldn't you just cast the double to an int, like

int convertedFromDouble = (int)inputDouble;

 

Specs: CPU: AMD Ryzen R7 3700X @4.4Ghz, GPU: Gigabyte RX 5700 XT, RAM: 32 GB (2x 8GB Trident Z Royal + 2x 8GB TForce Vulkan Z) @3000Mhz, Motherboard: ASRock B550m Steel Legend, Storage: 1x WD Black 1Tb NVMe (boot) + 1x Samsung 860 QVO 1Tb SSD (storage), Case: Thermaltake Core V21, Cooler: Noctua NH-D15

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Adorable Cat said:

Couldn't you just cast the double to an int, like


int convertedFromDouble = (int)inputDouble;

 

Isn't this also possible with parse somehow? Thanks for your help

Link to comment
Share on other sites

Link to post
Share on other sites

Integer.parseInt() is for strings. You can just use (int) as @Adorable Cat mentioned

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, reniat said:

Integer.parseInt() is for strings. You can just use (int) as @Adorable Cat mentioned

So String to Double, String to float or String to Int = parse? And Double to Int or float to int = (type) X ?

Link to comment
Share on other sites

Link to post
Share on other sites

19 minutes ago, Hip said:

So String to Double, String to float or String to Int = parse? And Double to Int or float to int = (type) X ?

Yes kind of, but think about it less like "when casting I use ____ for this type", remember that parse is a method on Integer (or Double, or Float). 

This syntax:

NewType someValue = (NewType) oldValue;

is the only way to cast that's built directly into the language. The parse methods on Float, Integer, and Double are just methods used to make objects of those types given other data. They aren't "casting" so much as they are almost like static constructors, where you give it a double and it gives you a new Integer object. parse could also be renamed Integer.createFromString(String s); and it'd mean the same thing.

 

The difference is that casting is simply changing the reference to the object, while methods like these parse methods aren't as limited by the original. For example, you could make a parse method that had custom logic, like:
 

static int parsePositive(double d) {
    return (d < 0) ? 0 : (int) d;
}

// or if you're not familiar with ternaries:
static int parsePositive(double d) {
    if (d < 0) {
        return 0;
    }
    return (int) d; 
}

 

And raw casting as built by the language doesn't provide that level of customization, because it's simply changing reference types.

 

So while yes, casting does let you take an object of one type and create an object of a different type from that same object, i would not call it accurate to say that parse() is "casting" here, as there is no valid general transformation from string to numeric (which is why you can't just use the normal cast), though this probably just down to semantics.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

35 minutes ago, reniat said:

Yes kind of, but think about it less like "when casting I use ____ for this type", remember that parse is a method on Integer (or Double, or Float). 

This syntax:


NewType someValue = (NewType) oldValue;

is the only way to cast that's built directly into the language. The parse methods on Float, Integer, and Double are just methods used to make objects of those types given other data. They aren't "casting" so much as they are almost like static constructors, where you give it a double and it gives you a new Integer object. parse could also be renamed Integer.createFromString(String s); and it'd mean the same thing.

 

The difference is that casting is simply changing the reference to the object, while methods like these parse methods aren't as limited by the original. For example, you could make a parse method that had custom logic, like:
 


static int parsePositive(double d) {
    return (d < 0) ? 0 : (int) d;
}

// or if you're not familiar with ternaries:
static int parsePositive(double d) {
    if (d < 0) {
        return 0;
    }
    return (int) d; 
}

 

And raw casting as built by the language doesn't provide that level of customization, because it's simply changing reference types.

 

So while yes, casting does let you take an object of one type and create an object of a different type from that same object, i would not call it accurate to say that parse() is "casting" here, as there is no valid general transformation from string to numeric (which is why you can't just use the normal cast), though this probably just down to semantics.

 

Ok that's a kinda complicated explanation to me because I don't know that much yet lol

I tried Integer.createFromString(String s) and it didn't work for me somehow.

Link to comment
Share on other sites

Link to post
Share on other sites

31 minutes ago, Hip said:

and it didn't work for me somehow.

Sorry I may have over-explained a bit. Integer.createFromString() was an example I made up, illustrating that .parse() is just another method, not something baked into the language like casting using parenthesis like (int)

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, reniat said:

Sorry I may have over-explained a bit. Integer.createFromString() was an example I made up, illustrating that .parse() is just another method, not something baked into the language like casting using parenthesis like (int)

Now I understand thanks.

Can you answer me another question, I want to print something in System.our.println and somehow I get errors like "error: ';' expected" "error: not a statement" and " error: ')' expected"

 

This is the outprint:

 System.out.println("double: "+doubleWert"\nbyte: "+byteWert);

 

It only occures when I put in \n

 

I solved it with:

System.out.println("double: "+doubleWert+"\n"+"byte: "+byteWert);

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 4/22/2019 at 10:06 AM, Adorable Cat said:

Couldn't you just cast the double to an int, like


int convertedFromDouble = (int)inputDouble;

 

This is my favourite (because ease) method but... if the double has a value like 10.5 wouldn’t that cause an issue? Or would the parsing drop the decimal? 

Link to comment
Share on other sites

Link to post
Share on other sites

56 minutes ago, fpo said:

This is my favourite (because ease) method but... if the double has a value like 10.5 wouldn’t that cause an issue? Or would the parsing drop the decimal? 

double 10.5 would convert to int 10

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×