How to pass variables of type String to int, float and double


In the past two articles we learned how to display messages and receive data from the dialogs box.

However, we note that the dialogs see everything as String type.
We will teach now, in our Java course how to transform what was typed as string to integer, float, double ...



How to transform a string in integer in Java

Suppose, by the method showInputDialog seen in past article, we asked a number to the user.
Ok! You asked, but it was stored in a String 'numberString'.

To pass this string to integer, we use the method of the String class called parseInt, from the static class Integer.
Suppose we store this integer value in 'numInteger', the code to do this would look like this:
numInteger = Integer.parseInt (numberString);

As an example, let's create an application that asks the user two integers and show the the sum as result:

import javax.swing.JOptionPane;

public class graphicSumm {
    
    public static void main(String[] args){
        String value, result;
        int num1, num2;
        
        value = JOptionPane.showInputDialog("Enter the first integer");
        num1 = Integer.parseInt(value.trim());
        
        value = JOptionPane.showInputDialog("Enter the second integer");
        num2 = Integer.parseInt(value.trim());
        
        result=String.format("Result: %d", num1+num2);
        JOptionPane.showMessageDialog(null,resultado);
    }

}



Most courses, tutorials and books out there simply would be happy to teach you how to:
num1 = Integer.parseInt (value);

Without this ' trim()'
Take a test. Enter '1 ', that's right, with a space before or after.
You'll have a hell of a mistake. And there?

'What bad program!'
Think any user who unwittingly put a space and see this error message.

The Progressive Java course aims to teach you to create real applications, things I learned in practice and no website or book told me.
This ' trim()' removes spaces from a string because SPACES AREN'T IMPORTANT IN A  NUMBER FIELD!
With the ' trim()' you leave your application more robust proof of nonsense users! And believe me, they are very creative!

Transforming string to float

numFloat = Float.parseFloat (numberString);


Transforming string in double

numDouble = Double.parseDouble (numberString);



Java exercise to use Dialog Boxes


We proposed and made an application that receives the coefficients of a quadratic equation and returns its roots, to complex, check out:
Application that give us the roots of the quadratic equation

After I proposed, in the exercises of methods to do the same, but breaking it in methods, to be reusable:
Exercises to training the use of methods in Java

Now do the same. A program that receives the coefficients of a quadratic equation and returns its roots, the same complex, but through dialogs.
Note that this is a really useful program.
In the next section we will teach how to make your applications become 'executable' so you can send programs (graphics, of course) to your friends!


No comments: