Results 1 to 10 of 10
  1. #1
    lzw
    lzw is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default java.util.Scanner NoSuchElementException

    Hi everyone.
    Scanner scan = new Scanner(System.in);
    while(true){
    String ll = scan.nextLine();
    if(ll.equals("exit"))
    break;
    }
    Above is my code, I run it work well in windows 7,but it run in linux throw exception.
    Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1516)
    at com.ctfo.move.AlarmStat.main(AlarmStat.java:57)
    I don't know why it is? please help me?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: java.util.Scanner NoSuchElementException

    According to the API:
    "
    NoSuchElementException - if no line was found
    "
    which is why the hasNextLine method exists.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    lzw
    lzw is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: java.util.Scanner NoSuchElementException

    I hope it type message from console,then take input value to process anything.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: java.util.Scanner NoSuchElementException

    Use the hasNextLine method to check whether there is a next line before reading it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: java.util.Scanner NoSuchElementException

    So if I have to believe the results had in this thread, scanning from System.in won't block on Linux? I don't have a Linux installation handy to test it out on :/

    Of course it may also be the version of Linux / the shell used that might make a difference.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    lzw
    lzw is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: java.util.Scanner NoSuchElementException

    I use hasNextLine method to check,scanning from system.in won't block on Linux,but used it windows work well.whether it has any other method to block in Linux.

  7. #7
    lzw
    lzw is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: java.util.Scanner NoSuchElementException

    You said right,if it isn't work, whether has any other method to block for working well in Linux.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: java.util.Scanner NoSuchElementException

    That code up there does not use hasNextLine.
    Show us the code that does use that and that fails on Linux.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    lzw
    lzw is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: java.util.Scanner NoSuchElementException

    code
    Scanner scan = new Scanner(System.in);
    System.out.println("Pls type start date:yyyy-MM-dd");

    String startDate = scan.nextLine();
    System.out.println("Pls type end date:yyyy-MM-dd");
    String endDate = scan.nextLine();
    Exception
    Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1516)
    at com.ctfo.move.AlarmStat.main(AlarmStat.java:57)

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: java.util.Scanner NoSuchElementException

    Which doesn't use hasNextLine.
    You're just calling nextLine without checking if there is a next one, which seems to fail on some systems.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. java.util.NoSuchElementException
    By Xyexs in forum New To Java
    Replies: 3
    Last Post: 04-19-2013, 03:09 PM
  2. java.util.NoSuchElementException
    By tabishis in forum New To Java
    Replies: 1
    Last Post: 10-12-2012, 05:30 AM
  3. java.util.NoSuchElementException
    By Parkuss in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 08:33 AM
  4. Replies: 6
    Last Post: 03-11-2011, 04:53 AM
  5. Replies: 5
    Last Post: 02-27-2009, 06:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •