Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!