Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何打印每行的第一个单词?_Java_String_Arraylist_Bufferedreader - Fatal编程技术网

Java 如何打印每行的第一个单词?

Java 如何打印每行的第一个单词?,java,string,arraylist,bufferedreader,Java,String,Arraylist,Bufferedreader,我有一个文本文件,如下所示: 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) { Bu

我有一个文本文件,如下所示:

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();
        }
    }
}
publicstaticvoidmain(字符串[]args){
BufferedReader reader=null;
ArrayList myFileLines=新的ArrayList();
试一试{
弦电流线;
读卡器=新的BufferedReader(新的
FileReader(“/Users/FakeUsername/Desktop/GroceryList.txt”);
而((sCurrentLine=reader.readLine())!=null){
System.out.println(sCurrentLine);
}
}捕获(IOE异常){
e、 printStackTrace();
System.out.print(如getMessage());
}最后{
试一试{
if(reader!=null)reader.close();
}捕获(IOEX异常){
System.out.println(例如getMessage());
例如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
函数。它根据要与字符串拆分的字符返回字符串数组。在您的情况下,如下所示

 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
的方法非常轻松地执行此操作:

String filename = "Your filename";
reader = new BufferedReader(new FileReader(fileName));
reader.lines()
      .map(line -> line.split("\\s+")[1])
      .forEach(System.out::println);
输出:



这将创建一个
,其中包含
BufferedReader
中的所有行,将每行分割为空白,然后获取第二个标记并打印它

查找实际上您只想打印单词[1],因为不应打印数字。
Bananas
Pudding
Soda
Bread