Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

225
Visualizações
InputMismatchException Java first scanner works second does not

This code is working for the first scanner to accept ints but the second scanner throws a mismatch error. I cannot seem to find why it is doing this. Can anyone help me out? I have tried everything and it is not working. I can get the first scanner to accept strings multiple times. The second scanner will accept ints but if i try to input a string even one time then the program crashes. How can this be resolved?

For more details of full program click here:

pastebin.com/iMgNncMH

Password: ENdu4mWLNm

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        
        while(true) {
        System.out.println("Enter two integers:");
        while (!scanner.hasNextInt()) {
            scanner.next();
            
        }

        
        int n1 = scanner.nextInt();
        int n2 = scanner.nextInt();
        
        System.out.println();
        
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I looked through the code, and it is working fine on BlueJ Here is the console image. Could you explain a little more about what error you are getting while compilation? Also here's the full code

import java.util.*; 

public class prime {

    public static void main(String[] args) { 
        Scanner scanner = new Scanner(System.in);
        
        while(true) {
            System.out.println("Enter two integers:");
            while (!scanner.hasNextInt()) {
                scanner.next();
            }
            int n1 = scanner.nextInt();
            int n2 = scanner.nextInt();
            
            System.out.println();
        }
    }
 }
over 4 years ago · Santiago Trujillo Relatório

0

The error being thrown is InputMismatchException. Its happening on the n2 why this is happening... idk I'm about as surprised as you are. Weird how it doesn't throw on n1 its the same issue. Perhaps it has something to do with the next int your looking for. Here's what I would suggest. Basic error handling using a Boolean while loop. If the catch is thrown it will never hit that true so two integers must be entered.

public static void main(String[] args) { 
    Scanner scanner = new Scanner(System.in);
    
        int n1 = 0;
        int n2 = 0;
        System.out.println("Enter two integers:");
        boolean validEntry = false;
        while(!validEntry) {
            try {
                n1 = scanner.nextInt();
                n2 = scanner.nextInt();
                validEntry = true;
            }
            catch(InputMismatchException ex){
                System.out.println("Please refrain from entering words. only numbers aloud");
                scanner.nextLine();
            }
        }
}

Ps that other while loop you had is never hit. I can see what you were going for though.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda