1 / 7

Converting string to integer

Converting string to integer. class StringToInt { public static void main (String arg[]) { // Assume arg[0] is the input string int result = 0, pos; int len = arg[0].length(); char c; for (pos = 0; pos < len; pos++) {

evangelinew
Download Presentation

Converting string to integer

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Converting string to integer class StringToInt { public static void main (String arg[]) { // Assume arg[0] is the input string int result = 0, pos; int len = arg[0].length(); char c; for (pos = 0; pos < len; pos++) { c = arg[0].charAt(len-pos-1); if ((c >= ‘0’) && (c <= ‘9’)) { result += ((c-’0’)*Math.pow(10, pos)); } // continued in next slide

  2. Converting string to integer else if ((c==‘-’) && (pos==len-1)) { result = -result; } else { System.out.println(“Invalid input: ” + arg[0]); break; } } // end for if (pos==len) { System.out.println(“Integer value: ” + result); } } }

  3. valueOf public class ValueOfDemo { public static void main(String[] args) { //this program requires two arguments on the command line if (args.length == 2) { //convert strings to numbers float a = (Float.valueOf(args[0]) ).floatValue(); float b = (Float.valueOf(args[1]) ).floatValue(); //do some arithmetic System.out.println("a + b = " + (a + b) ); System.out.println("a - b = " + (a - b) ); System.out.println("a * b = " + (a * b) ); System.out.println("a / b = " + (a / b) ); System.out.println("a % b = " + (a % b) ); } else { System.out.println("This program requires two command-line arguments."); } } }

  4. parseXXXX() • Note: Each of the Number subclasses that wrap primitive numeric types also provides a parseXXXX() method (for example, parseFloat()) that can be used to convert strings to primitive numbers. Since a primitive type is returned instead of an object, the parseFloat() method is more direct than the valueOf() method. For example, in the ValueOfDemo program, we could use: • float a = Float.parseFloat(args[0]); • float b = Float.parseFloat(args[1]);

  5. Converting Numbers to Strings There are several easy ways to convert a number to a string: int i;String s1 = "" + i; //Concatenate "i" with an empty string; //conversion is handled for you. or String s2 = String.valueOf(i); //The valueOf class method. Each of the Number subclasses includes a class method, toString(), that will convert its primitive type to a string. For example: int i;double d; String s3 = Integer.toString(i); String s4 = Double.toString(d);

  6. ToStringDemo public class ToStringDemo { public static void main(String[] args) { double d = 858.48; String s = Double.toString(d); int dot = s.indexOf('.'); System.out.println(dot + " digits before decimal point."); System.out.println( (s.length() - dot - 1) + " digits after decimal point."); } } • The output of this program is: 3 digits before decimal point. 2 digits after decimal point.

  7. Reversing a string class StringReverse { public static void main (String arg[]) { // Assume that the input is arg[0] String reversed = “”; int pos; for (pos=arg[0].length()-1; pos >= 0; pos--) { reversed += arg[0].charAt(pos); } System.out.println (“Original: ” + arg[0] + “, Reversed: ” + reversed); } }

More Related