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 need to make clone of an android app, I need to Make clone of an Android ...

I need to Make clone of an Android app Project Description: Make a total duplicate of the App: Camp and RV Campgrounds Plus The only difference will be the name (as it

What is includeaction, The IncludeAction class is useful when you require t...

The IncludeAction class is useful when you require to integrate Struts into an application that uses Servlets. Use the IncludeAction class to contain another resource in the respon

Week 7, This is a two part project, one in Alice, and another in Java. You ...

This is a two part project, one in Alice, and another in Java. You can either zip the files together as one and upload the single zip file, OR upload the separate files. If you dec

What is javaserver faces expression language, A easy expression language us...

A easy expression language used by a JavaServer Faces UI component tag attributes to bind the associated component to a bean property or to bind the associated component's value to

Difference between an interface and an abstract class, What is the differen...

What is the difference between an Interface and an Abstract class? An abstract class can have instance methods that execute a default behavior. An Interface can only declare co

Project on image processing, Project Description: Computer vision and Im...

Project Description: Computer vision and Image processing project to extract book title Skills required: Java, C Programming

Bluej, how to write a program to assign value of variable age and print if ...

how to write a program to assign value of variable age and print if person is major.

What is invoking static methods, What is Invoking static methods If a m...

What is Invoking static methods If a method or field is declared static, you access it through using the class name rather than a reference to a particular example of the class

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