[英]what's the point of using scanners in java (android)
Let me explain in detail, I kept googling about scanners and I can't fully understand what are scanners exactly. 让我详细解释一下,我一直在搜寻有关扫描仪的信息,但我无法完全完全了解什么是扫描仪。 I read many articles but all of them say 我读了很多文章,但所有人都说
The java.util.Scanner class is a simple text scanner which can parse primitive types and strings using regular expressions java.util.Scanner类是一个简单的文本扫描器,可以使用正则表达式解析原始类型和字符串
quoted from the official website, most websites took it and non of them said what a scanner is in english 从官方网站上引用的信息,大多数网站都接受了,其中没有一个说英语的扫描仪是什么
Let me illustrate. 让我举例说明。
I have 3 views, a Button
, EditText
, TextView
. 我有3个视图,一个Button
, EditText
, TextView
。 I wanted to take the text from the EditView
and put it in the TextView
and I have 2 approaches, my question is what is the difference between them. 我想从EditView
获取文本并将其放入TextView
,我有2种方法,我的问题是它们之间的区别是什么。
ALL THIS CODE GOES INTO THE ONCLICK LISTENER. 所有这些代码都将发送到点击列表中。
Scanner sc = new Scanner(editText.getText().toString());
String a = sc.next();
txv.setText(a);
and this txv.setText(editText.getText().toString());
和这个txv.setText(editText.getText().toString());
I got the data and it worked exactly the same in both cases and I can't seem to find anything useful. 我得到了数据,在两种情况下它的工作原理完全相同,而且似乎找不到任何有用的东西。
You shouldn't be using Scanner here, you should be using StringTokenizer, or just split("\\\\s+")[0]
on the string. 您不应该在这里使用Scanner,而应该使用StringTokenizer,或者仅对字符串进行split("\\\\s+")[0]
。
But sc.next()
will read the first consecutive non-whitespace characters of the Scanner's input string into the variable a
, which gets set to the next textview. 但是sc.next()
会将Scanner输入字符串的第一个连续的非空白字符读取到变量a
,该变量将设置为下一个textview。
For example 例如
Scanner sc = new Scanner("hello world");
String s = sc.next(); // == "hello"
Otherwise, in CLI applications Scanner is used for interactive inputs. 否则,在CLI应用程序中,将Scanner用于交互式输入。 Doesn't have much other use cases in production code that I can think of. 我能想到的在生产代码中没有很多其他用例。 Even for reading files, BufferedReader or the NIO API would be preferred 即使是读取文件,最好还是使用BufferedReader或NIO API
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.