How to convert string value to number in java, JAVA Programming

Assignment Help:

How to convert string value to number in java?

While processing user input it is frequent essential to convert a String in which the user enters into an int. The syntax is straightforward. It needs using the static Integer.valueOf(String s) and intValue() methods from the java.lang.Integer class. To convert the String "22" into the int 22 you would write

int i = Integer.valueOf("22").intValue();

Doubles, floats and longs are converted similarly. To convert a String like "22" into the long value 22 you would write

long l = Long.valueOf("22").longValue();

To convert "22.5" into a float or a double you would write:

double x = Double.valueOf("22.5").doubleValue();
float y = Float.valueOf("22.5").floatValue();

The several valueOf() methods are relatively intelligent and can handle plus and minus signs, exponents, and most other general number formats. However if you pass one something fully non-numeric like "pretty in pink," it will throw a NumberFormatException. You haven't learned how to handle exceptions yet, so try to avoid passing theses techniques non-numeric data.

You can now rewrite the E = mc2 program to accept the mass in kilograms as user input from the command line. Several of the exercises will be same.

class Energy {
  public static void main (String args[]) {
     double c = 2.998E8;  // meters/second
    double mass = Double.valueOf(args[0]).doubleValue(); 
    double E = mass * c * c;
    System.out.println(E + " Joules");
  }
}

Here's the output:

$ javac Energy.java
$ java Energy 0.0456
4.09853e+15 Joules

Related Discussions:- How to convert string value to number in java

I want selenium based test automation for java, I want Selenium Based Test ...

I want Selenium Based Test Automation for Java Based Web Application Project Description: I want some test cases to be automated to login to UI which is Java based web applic

Explain the rules for tostring() methods, Explain the Rules for toString() ...

Explain the Rules for toString() Methods ? toString() methods should return a single line of text that does not contain any carriage returns or linefeeds. toString() methods

Swing, can we develop paint user can draw his charcters and images in swing...

can we develop paint user can draw his charcters and images in swing

What are the principle concepts of oops, What are the principle concepts of...

What are the principle concepts of OOPS? Principle concepts upon that object oriented programming and design rest. They are: Abstraction: Abstraction refers to the act of

Creating applications based on soa, Creating applications based on SOA ...

Creating applications based on SOA Select your implementation technologies carefully. Carefully check if Web services are suitable. Sometimes using traditional methods suc

What system.out.println() signifies, What 'System.out.println()' signifies?...

What 'System.out.println()' signifies? 'System' is a predefined class. System class gives access to the system. 'out' is the output stream. 'println' is printin

Write down html or javascript code to declare array, Write down HTML or Jav...

Write down HTML or JavaScript code for the following requirements: Declare an Array along with the identifier SUBJECT having length 5. Assign subsequent values to the array par

Develop an android app for me, Project Description: I need someone to pr...

Project Description: I need someone to prepare an android app for me and provide it to me asap. The app details to be provided on pm Skills required: Java, Android

Program to calculates the number of unique shapes, 1. Write the code for th...

1. Write the code for the method that calculates the number of unique shapes for a binary search tree with n nodes. Use the formula  where b n is the number of possible sh

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd