Java.Util.Nosuchelementexception: No Line Found

java.util.NoSuchElementException: No line found

with Scanner you need to check if there is a next line with hasNextLine()

so the loop becomes

while(sc.hasNextLine()){
str=sc.nextLine();
//...
}

it's readers that return null on EOF

ofcourse in this piece of code this is dependent on whether the input is properly formatted

how to fix : Exception in thread main java.util.NoSuchElementException: No line found

You're creating two Scanner objects based on System.in.

Each Scanner will read from System.in and consume some of its input. That means that the first one might have already read some things into its buffer when you created the second one.

The short answer is: never create multiple Scanner objects based on the same parameter, simply continue using the existing scanner. For example this would fix your issue:

public static void main(String[] args) {  
Scanner myScanner = new Scanner(System.in);
String userName = myScanner.nextLine();
System.out.print("Username is: " + userName);


String number = myScanner.nextLine();
// ... rest of your code ...

NoSuchElementException : No line found

You are probably using an online java code editor/compiler which does not have an stdin input.

As stated by arcy, you are probably using an IDE with a built-in console window which allows you to pass standard inputs to your program.

The following online editor will allow you to add inputs.
You should take care of the way you are using the nextLine method of Scanner:

The exception you are getting is the result of the scanner not getting any inputs, as can be seen here. I suggest you refactor your loop on the scanner using the method hasNextLine of Scanner.

Exception in thread main java.util.NoSuchElementException: No line found

The API document of readLine() method says as below

Throws:
NoSuchElementException - if no line was found

You are supposed to handle this exception or just use the hasNextLine() method to avoid the exception.

while(sc.hasNextLine()){
requestedSeat = sc.nextLine();
}

I'm getting java.util.NoSuchElementException: No line found Error

Your code is fine. You just need to make a word either lowercase or uppercase before comparing.

static boolean isPalindrome(String word){
word=word.toLowerCase(); //Added this
int c=word.length()-1;
boolean flag = false;
for(int i=0; i<word.length(); i++){
if(word.charAt(i)==word.charAt(c))
flag=true;
else
return false;
c--;
}
return flag;
}


Related Topics



Leave a reply



Submit