The great trick is to use the math module function or absolute value to print the that is desired. Because it is a fairly common function to find it in most programming languages in Java, the same is defined in Math.abs () and abs () is the acronym for absolute in English.
In this way your code could use this way:
public class Main {
public static void main(String[] args) {
int i1 = -10;
//Pega o valor absoluto de i1 isto é o módulo do mesmo
int i2 = Math.abs(i1);
System.out.println(i1);
System.out.println(i2);
}
}
Another way to do this would be through regular expressions, but it would not make sense to use a regular expression and conversions just to pick up the module from a number since this operation would be very costly ):
public class Main {
public static void main(String[] args) {
int i1 = -10;
String regex = "" + i1;
regex = regex.replaceAll("[^0-9]", "");
int i2 = Integer.parseInt(regex);
System.out.println(i2);
}
}
Note that it's pretty much the same thing if you do with replace:
regex = regex.replaceAll("-", "");
or
regex = regex.substring(1); //retira o primeiro caractere
Although all of these methods work does not make up for creating a String object only to do a conversion when Java already provides the function in the Math Library.