You are not logged in. Please login at www.codechef.com to post your questions!

×

NoSuchElementException for nextInt

compiling below code gives an error, can someone help me in resolving this?

import java.util.*; import java.lang.*; import java.io.*; import java.util.Scanner;

/ Name of the class has to be "Main" only if the class is public. /
class Codechef
{
public static void main (String[] args)
{
int a,b;
Scanner input = new Scanner(System.in);
a=input.nextInt();
b=input.nextInt();
int diff = 0, output1 = 0, output2 = 0, diffDigits =0;
diff = a-b;
}
}

asked 27 May '18, 20:08

vangari's gravatar image

0★vangari
1
accept rate: 0%


just give proper inputs and run it... U wont get any error :)
JAVA yells when it doesn't get required inputs...

link

answered 28 May '18, 02:55

l_returns's gravatar image

5★l_returns
1.4k19
accept rate: 25%

import java.util.;
import java.lang.
;
import java.io.*;

class Codechef
{
public static void main (String[] args)
{
int a,b;
Scanner input = new Scanner(System.in);
a=input.nextInt();
b=input.nextInt();
System.out.println(a-b);
}
}

above code is still giving below error and NZEC as Runtime error
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 Codechef.main(Main.java:11)

link

answered 29 May '18, 11:55

vangari's gravatar image

0★vangari
1
accept rate: 0%

Read my answer.... Give inputs to compiler and error will vanish.. and you are getting wrong answer because your logic is incorrect..

(29 May '18, 12:01) l_returns5★

Try using BufferedReader. Worked out fine for me. A scenario you have to keep in mind is if the user enters NULL value i.e. simply presses enter. In that case you should surround your input in a Try block, while Catch just returns.

Ex :

try {cases = Integer.parseInt(br.readLine());} catch(Exception e) {return;}

link

answered 09 Feb, 17:42

harmanrawal's gravatar image

2★harmanrawal
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×422
×13
×1
×1

question asked: 27 May '18, 20:08

question was seen: 567 times

last updated: 09 Feb, 17:42