Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello good people, Happy new year.
I was trying to solve a problem, logically I solved it and implemented it in Java. I use eclipse IDE to write and test Java program.
I ran the program and it worked fine, giving the expected answer for different testcases.
But when I submit my solution to codeforces(a competitive programming website), I get runtime error on test 1.

Codeforces judge uses Java SE 17.

java.util.NoSuchElementException

	at java.base/java.util.Scanner.throwFor(Scanner.java:937)

	at java.base/java.util.Scanner.next(Scanner.java:1594)

	at java.base/java.util.Scanner.nextLong(Scanner.java:2373)

	at java.base/java.util.Scanner.nextLong(Scanner.java:2328)

	at Main.solve(Main.java:9)

	at Main.main(Main.java:24)


Input

4

2

3

50

1000000000


I was trying to find out from yesterday and submitted more than 10 times with little little changing.. but nothing worked.
So, anyone here to help me?

What I have tried:

import java.util.Scanner;

public class Main {
	Scanner sc = new Scanner(System.in);
	
	void solve() {
		long n = sc.nextLong();
		long mod = 1000000007L;
		long ans=((((n*(n+1))%mod)*(4*n-1))%mod*(2022/6))%mod;
		System.out.println(ans);
		
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Main main = new Main();
		
		int t = sc.nextInt();
		
		while(t-->0) {
			
			main.solve();
			
		}
		
	}

}
Posted
Updated 3-Jan-23 1:14am
v2

1 solution

Remove the line package pac1; at the beginning. Most of these websites expect code to be in stand-alone form, not in packages.

[edit]
Try this:
Java
import java.util.Scanner;

public class Main {
	void solve(Scanner sc) {  // scanner passed in as parameter
		long n = sc.nextLong();
		long mod = 1000000007L;
		long ans=((((n*(n+1))%mod)*(4*n-1))%mod*(2022/6))%mod;
		System.out.println(ans);	
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Main main = new Main();
		
		int t = sc.nextInt();
		
		while(t-->0) {
			
			main.solve(sc);  // pass the scanner instance to the function
			
		}
		
	}

}


[/edit]
 
Share this answer
 
v3
Comments
Jahirul Sarker 3-Jan-23 7:00am    
Hello @Richard, thanks for responding. I removed that package line and submitted, but it doesn't solve the problem.. same runtime error happens and same message.
Richard MacCutchan 3-Jan-23 7:29am    
I cannot see exactly what the error is complaining about. The only thing that may be the cause is that you have created two scanner objects. Try removing the line Scanner sc = new Scanner(System.in); at line 15 in the main method.
Jahirul Sarker 3-Jan-23 10:39am    
Removing Scanner... from main method gives "Cannot make a static reference to the non-static field sc" message.. and can't run the program in eclipse
Richard MacCutchan 3-Jan-23 11:35am    
See my updated solution.
Jahirul Sarker 3-Jan-23 11:52am    
It worked. I was really worried and kept submitting with little little change but couldn't figure out the problem. Your solution saved me. Thank you so much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900