This specific case can be worked with strings. I do not really like the floating-point alternative so much to make a response because of the floating point problem .
So, how to solve the problem? Well, reading a string. And working with string. And if holding only in string.
Considering that the return is an object of class PartesNumero
with the fields String parteInteira
and String parteDecimal
, we could do the following constructor:
public PartesNumero(String numeroCompleto) {
String partes[] = numeroCompleto.split("[^0-9]");
if ("".equals(partes[0]) {
this.parteInteira = 0; // fallback para entradas como ".75"
} else {
this.parteInteira = partes[0];
}
if (partes.length > 1) {
this.parteDecimal = partes[1];
} else {
this.parteDecimal = "";
}
}
And to read a string from the entry (not doing any type validation in the meantime):
Scanner sc = new Scanner(System.in);
String entrada = sc.next();
The complete program would then be:
public class Main {
public class PartesNumero {
public final String parteInteira;
public final String parteDecimal;
public PartesNumero(String numeroCompleto) {
String partes[] = numeroCompleto.split("[^0-9]");
if ("".equals(partes[0]) {
this.parteInteira = 0; // fallback para entradas como ".75"
} else {
this.parteInteira = partes[0];
}
if (partes.length > 1) {
this.parteDecimal = partes[1];
} else {
this.parteDecimal = "";
}
}
}
public static void main(String []args) {
Scanner sc = new Scanner(System.in);
String entrada = sc.next();
PartesNumero partes = new PartesNumero(entrada);
System.out.println(partes.parteInteira);
System.out.println(partes.parteDecimal);
}
}