获取引号

时间:2016-03-02 01:10:31

标签: java file-io printwriter

import java.io.File; 
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class excersie2
{
    @SuppressWarnings("resource")
    public static void main(String[] args) throws FileNotFoundException
    {

    ArrayList<String> titles = new ArrayList<String>();
    ArrayList<String> titlestwo = new ArrayList<String>();

    String path = "C:\\Users\\T-450s\\Desktop\\showtimes.txt";
    File showtimes = new File(path);

    Scanner fileReader = new Scanner(showtimes);

    PrintWriter writer = new PrintWriter("C:\\Users\\T-450s\\Desktop\\output.txt");
    while(fileReader.hasNextLine())
    {
        titles.add(fileReader.nextLine()); 
    }

    String movie = "";
    for(String title : titles)
    {
        movie = title.substring((title.indexOf("\"")+1), title.lastIndexOf("\"")); 
    }

    writer.println("\n" + movie);
    writer.close();
}
}

读取以下txt文件:

Now Showing

Title: "Deadpool"

Duration: 1 hr 48 mins

Title: "Zoolander"

Duration: 1 hr 42 mins

Title: "Kung Fu Panda 3"

Duration: 1 hr 35 mins

and writes a new file containing:

Deadpool

Zoolaner

Kung Fu Panda 3

我如何实现这一目标?我尝试使用子字符串,但每次都会出错。

1 个答案:

答案 0 :(得分:2)

我不确定这是否是你得到的错误但我认为是,我认为你得到一个错误,因为子字符串试图找到一个&#34;但是可以在某些行上,因为它不存在并导致错误,所以你可以做的是用{ifer语句'来包围movie = substring(...以检查&#34; title&# 34;包含一个&#34;,你可以这样做if(title.contains(" \""))你也应该把writer.println放在for循环中