Exception error in thread "main" java.lang.Numberformatexception: For input string:

Asked

Viewed 85 times

-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);
            }
        
        }
}

1 answer

1

Speak, Otávio. Your code just needs a few adjustments.

In this case the ideal is that you validate cnpj before formatting it. You also don’t need the for.

Below, I put a solution for you. See if it meets your need.

Obs.: I just commented on the section you will no longer need and repositioned the if.

    Locale.setDefault(Locale.US);
    sc = new Scanner(System.in);

    System.out.println("Digite seu cnpj: ");
    String cnpj = sc.nextLine();
    int n = cnpj.length();

    // validando cnpj antes de formata-lo
    if (n != 14) {
        System.out.println("cnpj invalido, digite um formatado valido");
        System.exit(0);
    }
    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 {
     * 
     * } não precisa do for :)
     * 
     */

    System.out.println("O CNPJ sem formatação é: " + cnpj);
    System.out.println("O CNPJ formatado é: " + cnpjFormatado);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.