Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

308
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda