Hi,
i’m new in codechef, i’m doing SIXFRIEND.
I submitted the code, in eclipse runs fine with same input as codechef, but in codechef it is not working, this error appear:
Exception in thread “main” java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at Codechef.main(Main.java:18)
Thank you, i’ve tried modifing the code multiple times but stil does not work.
Hey @michelangelo_f
,
Thanks for asking your doubt. It’s hard to say without checking the code can you please send your code.
i didn’t post the code cause the contest was an ongoing contest.
Here’s the code:
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef {
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
int testcases = sc.nextInt();
int i = 0;
String[] arr = new String[2];
for (int count = 0; count < testcases; count++) {
String input = sc1.nextLine();
String[] temparr = input.split(" ");
for (String a : temparr) {
arr[i] = a;
i++;
}
i = 0;
int droom = Integer.parseInt(arr[0]) * 3;
int troom = Integer.parseInt(arr[1]) * 2;
if (troom > droom) {
System.out.println(droom);
} else {
System.out.println(troom);
}
}
}
}
Hey @michelangelo_f
,
Your logic is correct and the error is only on input phase.Try taking values using nextInt() every n times(for example :given an array of n size then run a loop for n times and take input this way).
for(loop till n times):
val = scanner_input_class.nextInt()
this is a pseudo code how you can take input for an array in java.
Here is your code I have modified with this changes.
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef {
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
int testcases = sc.nextInt();
int i = 0;
String[] arr = new String[2];
for (int count = 0; count < testcases; count++) {
int val1 = sc.nextInt(); // have taken 1st value in val1
int val2 = sc.nextInt(); // have taken 2nd value in val2
int droom = val1 * 3;
int troom = val2 * 2;
if (troom > droom) {
System.out.println(droom);
} else {
System.out.println(troom);
}
}
}
}