Issue with the online Judge system while having correct code and working solutions

Dear all,

I am submitting problems to the Judge system. While they are working perfectly in my IntelliJ IDE when I paste them in the Judge system and submit them - the system deems them wrong with exceptions.

For example the problem for Java - Rectangle Area

This is my source code:

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
Scanner user_input_width = new Scanner(System.in);
Scanner user_input_heigth = new Scanner(System.in);
String width;
String heigth;
System.out.println("Please enter width ");
width = user_input_width.next();
System.out.println(“Please enter height”);
heigth = user_input_heigth.next();
int x = Integer.parseInt(width);
int y = Integer.parseInt(heigth);
int area = x * y;
System.out.println(“The area of the rectangle is” + " " + area );

}
}
And this is the error I get.

Fixed it. :slight_smile:

I think you can try and optimize your code a little:

public class Main{
public static void main(String[] args) {
    Scanner user_input = new Scanner(System.in);
    int width;
    int height;

    System.out.println("Please enter width: ");
    width = user_input.nextInt();

    System.out.println("Please enter height: ");
    height = user_input.nextInt();

    int area = width * height;
    System.out.println("The area of the rectangle is " +  area );

}

}

First, you can create just one Scanner. No need to make two of them. Then you can create width and height as int variable instead of String, and when the user types in, you can use nextInt() which will expect an int value. That way there is no need to convert the String to int. :slight_smile:

2 Likes

Yeah, I did that. Suddenly the solution from 30 lines became like 5 lines of code :smiley:

1 Like

Hey Tedy,

What Ivan (koctech) suggested was not your problem but is more logical to use integers here nevertheless :slight_smile:
1st error: The text messages was messing with Judge. --> the platform expect simple input and output as in the Task description. Be careful with Judge it is strict.
2nd error was the second scanner
3rd the code pasted here was with mixed quotation marks :slight_smile:

Good Luck !

1 Like