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

Oop, creating a point of sale

creating a point of sale

How can we define a pixel, How can we define a Pixel? It is the smallest ...

How can we define a Pixel? It is the smallest number of element of image that is spread along with regular array on display and each constituent consist of particular color.

Need to modify joomla component, Project Description: I want to modify J...

Project Description: I want to modify Joomla component. Now there is Akeeba subscription component on this site. It works and the whole thing is fine, but there is no "teaser

Basic difference between java and c++, Both Java and C++ use same syntax an...

Both Java and C++ use same syntax and are Object Oriented, but there are several differences between Java and C++:   1.    Java does not use pointers. Pointers are inherently

Algorithms, COMPSCI220S2C2012 Assignment 1 due 8 August 8:30 pm This assign...

COMPSCI220S2C2012 Assignment 1 due 8 August 8:30 pm This assignment introduces you to an example of applied algorithm analysis and shows you why, sometimes, compromises in an algor

Please help, Write a program to find the area under the curve y = f(x) betw...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Java rmi client implement and web service client-side steps, What is the na...

What is the name of the services you have chosen? What do they do? What is the name of the publisher? Provide details if you are developing your own service(s). There are variou

Data access objects (DOA) in JDBC, Business Objects represent the data cl...

Business Objects represent the data client. They are the objects that need access to the datasource to obtain and store data. Data Access Objects abstract the relying data access i

Program for solving the producer consumer problem, For this assignment we w...

For this assignment we will be solving the producer-consumer problem with a bounded buffer. You are required to implement this assignment in Java. There are three components in thi

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