我将如何打印出每行的第一个单词?
How would I print out the first word of each line?
我有一个文本文件,内容如下:
1. Bananas that are not green
2. Pudding that is not vanilla
3. Soda that is not Pepsi
4. Bread that is not stale
我只是想让它打印出每行的第一个单词
不包括数字!
它应该打印为:
Bananas
Pudding
Soda
Bread
这是我的代码:
public static void main(String[] args) {
BufferedReader reader = null;
ArrayList <String> myFileLines = new ArrayList <String>();
try {
String sCurrentLine;
reader = new BufferedReader(new
FileReader("/Users/FakeUsername/Desktop/GroceryList.txt"));
while ((sCurrentLine = reader.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
System.out.print(e.getMessage());
} finally {
try {
if (reader != null)reader.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
}
请尝试以下代码-:
outerWhileLoop:
while ((sCurrentLine = reader.readLine()) != null) {
//System.out.println(sCurrentLine);
StringTokenizer st = new StringTokenizer(sCurrentLine," .");
int cnt = 0;
while (st.hasMoreTokens()){
String temp = st.nextToken();
cnt++;
if (cnt == 2){
System.out.println(temp);
continue outerWhileLoop;
}
}
}
使用String的split
函数。它 returns 根据我们要与字符串拆分的字符的字符串数组。在你的情况下,它就像下面这样。
String sCurrentLine = new String();
reader = new BufferedReader(new
FileReader("/Users/FakeUsername/Desktop/GroceryList.txt"));
while ((sCurrentLine = reader.readLine() != null) {
String words[] = sCurrentLine.split(" ");
System.out.println(words[0]+" "+words[1]);
}
Java 8+ 你可以使用BufferedReader
的lines()
方法很容易地做到这一点:
String filename = "Your filename";
reader = new BufferedReader(new FileReader(fileName));
reader.lines()
.map(line -> line.split("\s+")[1])
.forEach(System.out::println);
输出:
Bananas
Pudding
Soda
Bread
这将创建 BufferedReader
中所有行的 Stream
,在空白处拆分每一行,然后获取第二个标记并打印它
我有一个文本文件,内容如下:
1. Bananas that are not green
2. Pudding that is not vanilla
3. Soda that is not Pepsi
4. Bread that is not stale
我只是想让它打印出每行的第一个单词 不包括数字!
它应该打印为:
Bananas
Pudding
Soda
Bread
这是我的代码:
public static void main(String[] args) {
BufferedReader reader = null;
ArrayList <String> myFileLines = new ArrayList <String>();
try {
String sCurrentLine;
reader = new BufferedReader(new
FileReader("/Users/FakeUsername/Desktop/GroceryList.txt"));
while ((sCurrentLine = reader.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
System.out.print(e.getMessage());
} finally {
try {
if (reader != null)reader.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
}
请尝试以下代码-:
outerWhileLoop:
while ((sCurrentLine = reader.readLine()) != null) {
//System.out.println(sCurrentLine);
StringTokenizer st = new StringTokenizer(sCurrentLine," .");
int cnt = 0;
while (st.hasMoreTokens()){
String temp = st.nextToken();
cnt++;
if (cnt == 2){
System.out.println(temp);
continue outerWhileLoop;
}
}
}
使用String的split
函数。它 returns 根据我们要与字符串拆分的字符的字符串数组。在你的情况下,它就像下面这样。
String sCurrentLine = new String();
reader = new BufferedReader(new
FileReader("/Users/FakeUsername/Desktop/GroceryList.txt"));
while ((sCurrentLine = reader.readLine() != null) {
String words[] = sCurrentLine.split(" ");
System.out.println(words[0]+" "+words[1]);
}
Java 8+ 你可以使用BufferedReader
的lines()
方法很容易地做到这一点:
String filename = "Your filename";
reader = new BufferedReader(new FileReader(fileName));
reader.lines()
.map(line -> line.split("\s+")[1])
.forEach(System.out::println);
输出:
Bananas
Pudding
Soda
Bread
这将创建 BufferedReader
中所有行的 Stream
,在空白处拆分每一行,然后获取第二个标记并打印它