[英]Turning a line from a java scanner(file) into an array
All I am writting a simple java method that reads a text file of place names and will then map them to some characteristics...我正在编写一个简单的java方法,它读取地名的文本文件,然后将它们映射到一些特征......
This is the first time in java that I have read from a file.这是我第一次在 java 中从文件中读取。
I have written the code below that works - it reads the first line.我已经编写了下面的代码 - 它读取第一行。
But what I want to do is to turn this line into a character array so that I can compare each character to a unsorted file and then map the words.但是我想要做的是将这一行变成一个字符数组,这样我就可以将每个字符与一个未排序的文件进行比较,然后映射单词。
But I am struggling with a very basic challenge.但我正在努力应对一个非常基本的挑战。 How do I map the sc.scanner to a character array
如何将 sc.scanner 映射到字符数组
Or should I use BufferReader或者我应该使用 BufferReader
File file = new File ("c:/Users/Green/documents/places.txt");
Scanner sc = new Scanner(file);
// while (sc.hasNextLine())
System.out.println(sc.nextLine());
There is a method of String which is toCharArray()
.有一个 String 的方法是
toCharArray()
。
See the String documentation .请参阅字符串文档。
As documented the method Scanner.正如所记录的方法扫描仪。 nextLine returns a String, you can transform it to a character array using String.
nextLine返回一个字符串,您可以使用字符串将其转换为字符数组。 tocharArray and after iterate over it like below:
tocharArray并在迭代之后如下所示:
String line = sc.nextLine();
char[] arr = line.toCharArray();
for (char ch: arr) {
...here your logic
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.