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

307
Visualizações
How to make a multiplying program in Java using input line?

I tried to solve a problem on CodeChef. The problem should be multiplying two numbers. This is the script:

import java.util.*;
import java.lang.*;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int test;
        Scanner sc = new Scanner(System.in);
        test = sc.nextInt();
        while(test!=0){
            test = test - 1;
            int nummer1;
            int nummer2;
            nummer1 = sc.nextInt();
            nummer2 = sc.nextInt();
            int nummer3;
            nummer3 = nummer1 * nummer2;
            System.out.println(nummer3);
        }
    }
}

What my program needs to do is: first, get the amount of test cases, and then, it should multiply nummer1 and nummer2 to make nummer3. For example: INPUT

2
2 3
4 6

And the OUTPUT:

6
24

But the whole program is giving me a runtime error (NZEC). What am I doing wrong? It gives me this error:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:10)
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I would do it this way:

Scanner sc = new Scanner(System.in);
int test = Integer.parseInt(sc.nextLine());
while (test-- > 0) {
    String[] tmp = sc.nextLine().split("\\s+");
    int result = Integer.parseInt(tmp[0]) * Integer.parseInt(tmp[1]);
    System.out.println(result);
}
sc.close();
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