Java: .nextLine() and .nextDouble() differences

前端 未结 2 1272
耶瑟儿~
耶瑟儿~ 2020-12-07 03:01

I was reading the API for Java because I had a question on the difference between .nextLine() and .nextDouble(). In the API, it says this for

相关标签:
2条回答
  • 2020-12-07 03:41
    nextDouble()
    

    only reads the next 4 bytes from the input stream which make up the double and does not advance to the next line, whereas nextLine() does. Also i think you need to understand that a new line is designated by the string '\n', and obviously a number will not contain a linebreak, so 'nextDouble()' will not 'advance' to the next line.

    EDIT: Razvan is right

    0 讨论(0)
  • 2020-12-07 03:54

    It reads the next token (between 2 separating characters,white characters usually), not the next 4 bytes. It then transforms that token to a double if it can.

    Ex: Bla bla bla 12231231.2121 bla bla.
    

    The 4th token can be read using nextDouble(). It reads 13 chars and it transforms that string into a double if possible.

    0 讨论(0)
提交回复
热议问题