-2
I’m having trouble getting my program to return the IF in a correct way, when I put the FOR it counts the number of times it is in the variable, if I type an input less than 14 it instead of falling into the if of that error message:
Exception in thread "main" java.lang.NumberFormatException:
For input string: "11111111111111"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at application.Principal.main(Principal.java:17)
Similarly if I type a number much larger than 14 it returns the message I want that is in the IF.
Follows code:
package application;
import java.util.Locale;
import java.util.Scanner;
public class Principal {
public static void main(String args[]) {
Locale.setDefault(Locale.US);
Scanner sc = new Scanner(System.in);
System.out.println("Digite seu cnpj: ");
String cnpj = sc.nextLine();
int n = cnpj.length();
String cnpjFormatado = cnpj.substring(0, 2) + "." +
cnpj.substring(3, 5) + "." +
cnpj.substring(6, 8) + "/" +
cnpj.substring(9, 12)+ "-"+
cnpj.substring(13, 14);
for(int i = 0; i < cnpj.length(); i++ )
if (n !=14) {
System.out.println("cnpj invalido, digite um formatado valido");
return;
}
else{
//System.out.println(cnpj.replace(" ", "") + "\nO tamnho da string é: " +cnpj.length());
System.out.println("O CNPJ sem formatação é: " +cnpj);
System.out.println("O CNPJ formatado é: " +cnpjFormatado);
}
}
}