Testcases did not passed although worked correctly on custom input

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a=0,b=0,c=0;
while(sc.hasNext()) {
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
System.out.println(a+" β€œ+b+” "+c);
}

	int D = (b*b)-(4*a*c);
	if(a!=0) {
	if(D>0) {
		System.out.println("Real and Distinct");
		int d = (int)Math.sqrt(D);
		int x = (-b-d)/(2*a);
		int y = (-b+d)/(2*a);
		System.out.print(x+" "+y);
	}
	else if(D==0) {
		System.out.println("Real and Equal");
		int d = (int)Math.sqrt(D);
		int x = (-b-d)/(2*a);
		int y = (-b+d)/(2*a);
		System.out.print(x+" "+y);
	}
	else if(D<0) {
		System.out.println("Imaginary");
	}
	}
}

}

Hi @mudit2jain

There is no need to print the values of a, b and c in the start. Also, the while loop is not required too.

import java.util.Scanner;

import java.util.*;

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

	int a = sc.nextInt();
	int b = sc.nextInt();
	int c = sc.nextInt();
//	System.out.println(a + " " + b + " " + c);

	int D = (b * b) - (4 * a * c);
	if (a != 0) {
		if (D > 0) {
			System.out.println("Real and Distinct");
			int d = (int) Math.sqrt(D);
			int x = (-b - d) / (2 * a);
			int y = (-b + d) / (2 * a);
			System.out.print(x + " " + y);
		} else if (D == 0) {
			System.out.println("Real and Equal");
			int d = (int) Math.sqrt(D);
			int x = (-b - d) / (2 * a);
			int y = (-b + d) / (2 * a);
			System.out.print(x + " " + y);
		} else if (D < 0) {
			System.out.println("Imaginary");
		}

	}
}

I have made changes to your code. It should work fine now.

Actually i tried without while loop earlier but it gave me the following exception

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:9)

That is why i checked first with hasNext() function.
The code works fine when i run it with custom input with or without while() loop.
I have encountered this problem many times.

The meaning of NoSuchElementException means that you need to provide some custom input before running the code. Do that and your code will work fine.

What is the point of inbuilt testcases if i always have to provide my own custom input.
I thought testcases are to check our code under various constraints and to optimize our code further in case of failed testcases. But if i am providing my own custom input the code will run for that input only and how my code will be evaluated by your platform.

Hi Mudit

I am talking about the case when you are trying to run your code and not submitting it. When you Run your code without providing any custom input, this error occurs. But if you are submitting your code, then this error will not occur.

ok, thanks.
I got your point