Here I am trying to change a string to an integer and then return to an integer in a string.
Double slashes ('\\') are used to escape a special character, if they are used in several cases.
Here is the complete code snippet: Tested and Executed.
package com.siri; import java.text.NumberFormat; import java.text.ParseException; /* Java program to demonstrate how to implement static and non-static classes in a java program. */ class NumberFormat { // How to create instance of static and non static nested class? public static void main(String args[]) { NumberFormat numberFormat = NumberFormat.getInstance(); try { System.out.println(" number formatted to " + numberFormat.parse("123,456")); String numberToBeChanged="(123,456)"; if(numberToBeChanged.contains("(") || numberToBeChanged.contains(")")) { numberToBeChanged=numberToBeChanged.replaceAll("\\(", "").replaceAll("\\)", "").replaceAll(",", ""); int numberToBeChangedInt = Integer.parseInt(numberToBeChanged); numberToBeChangedInt *= -1; numberToBeChanged = Integer.toString(numberToBeChangedInt); } System.out.println(" number formatted to " + numberFormat.parse(numberToBeChanged)); } catch (ParseException e) { System.out.println("I couldn't parse your string!"); } } }
Now you see the expected results as indicated.
Sireesh yarlagadda
source share