# Java扫描器在竞争性编码挑战中是一个问题吗？

While I was solving this question with java (https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc7/00000000001d3f56)

I first attempted invoking `solution()` I defined outside `main()` but it kept giving me a runtime error. I had no clue why it makes an error and suspected it probably doesn't satisfy time limit requirement. So, I removed `solution()` and integrated into `main()` then it worked.

``````public class Solution {

public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int T = scn.nextInt();
String ans = "";

for (int i = 0; i < T; i++) {
ans += "Case #" + (i + 1) + ": " + sol() + "\n";
}
System.out.print(ans);

}

static int sol() {

Scanner sc = new Scanner(System.in);

int N = sc.nextInt();
int B = sc.nextInt();

int[] houses = new int[N];

for (int i = 0; i < N; i++) {
houses[i] = sc.nextInt();
}

Arrays.sort(houses);

int ans = 0;
for (int i = 0; i < N; i++) {

B = B - houses[i];
if (B < 0)
break;
ans++;
}
return ans;
}
}
``````

``````public class Solution2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
String ans = "";

for (int i = 0; i < T; i++){
int N = sc.nextInt();
int B = sc.nextInt();

int[] houses = new int[N];

for(int j = 0; j<N; j++)
houses[j]  = sc.nextInt();
Arrays.sort(houses);
int cnt = 0;
for(int j=0; j<N; j++){
B = B - houses[j];
if(B<0)
break;
cnt++;
}

ans+="\nCase #" + (i+1) + ": " + cnt;

}

System.out.println(ans);
}
}
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.5870557,"math_prob":0.9888671,"size":1477,"snap":"2021-43-2021-49","text_gpt3_token_len":472,"char_repetition_ratio":0.14663951,"word_repetition_ratio":0.24902724,"special_character_ratio":0.38524035,"punctuation_ratio":0.21794872,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.995033,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-23T04:39:26Z\",\"WARC-Record-ID\":\"<urn:uuid:35b5aaba-7c56-4859-b20b-8bd81a35ab58>\",\"Content-Length\":\"51515\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:16e7ec8a-366a-4525-90bc-8bb24c741c1b>\",\"WARC-Concurrent-To\":\"<urn:uuid:b2e8c358-7096-4d96-8a73-ea76fa8e6709>\",\"WARC-IP-Address\":\"47.52.26.78\",\"WARC-Target-URI\":\"https://mlog.club/article/5460663\",\"WARC-Payload-Digest\":\"sha1:7HVWMIB5V7KNMQ72736TS6AZI5ZUYPLX\",\"WARC-Block-Digest\":\"sha1:CWWAVZQB2UR6O4PHNL3OALGOQYWOPJJ6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585561.4_warc_CC-MAIN-20211023033857-20211023063857-00079.warc.gz\"}"}