本文介绍了Scanner类的next()和nextLine()方法有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! next()和 nextLine()之间的主要区别是什么? 我的主要目标是使用扫描程序读取所有文本,该文本可以连接到任何来源(例如文件)。What is the main difference between next() and nextLine()?My main goal is to read the all text using a Scanner which may be "connected" to any source (file for example).我应该选择哪一个以及为什么?Which one should I choose and why?推荐答案我总是喜欢阅读使用 nextLine()输入,然后解析字符串。I always prefer to read input using nextLine() and then parse the string.使用 next()只返回空格之前的内容。 nextLine()在返回当前行后自动将扫描器向下移动。Using next() will only return what comes before a space. nextLine() automatically moves the scanner down after returning the current line.用于解析来自 nextLine()将是 str.split(\\\\ +)。String data = scanner.nextLine();String[] pieces = data.split("\\s+");// Parse the pieces有关Scanner类或String类的更多信息,请参阅以下链接。For more information regarding the Scanner class or String class refer to the following links.扫描程序: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html字符串: http://docs.oracle.com/javase/7/docs/api/java /lang/String.html 这篇关于Scanner类的next()和nextLine()方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 07:00