2016-04-14 34 views
2

我正在編寫一個程序來更改輸入文件。它應該開始一個新的行後? 。和!但我似乎無法弄清楚。每一個新行也應該以我認爲我得到的大寫字母開頭。它也應該消除我也相信我得到的不必要的空間。如何在句號,問號和感嘆號之後開始換行?

例如:你好?酒保。我可以喝一杯威士忌嗎?

Output should be: 
Hello? 
Bartender. 
Can I have a drink!whiskey please. 

它應該只在這些運算符後面加一個空格後再換行。如果沒有空間,它不會換新線。

import java.util.Scanner; 
import java.io.*; 


public class TextFileProcessorDemo 
{ 
public static void main(String[] args) 
{ 
    String fileName, answer; 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Test Input File:"); 
    fileName = keyboard.nextLine(); 
    File file = new File(fileName); 

    PrintWriter outputStream = null; 

    try 
    { 
     outputStream = new PrintWriter(file); 
    } 
    catch(FileNotFoundException e) 
    { 
     System.out.println("Error opening file" + file); 
     System.exit(0); 
    } 

    System.out.println("Enter a line of text:"); 
    String line = keyboard.nextLine(); 
    outputStream.println(line); 
    outputStream.close(); 
    System.out.println("This line was written to:" + " " + file); 
    System.out.println(" "); 

    TextFileProcessor.textFile(); 


} 

} 

二等

import java.io.*; 
import java.util.Scanner; 


public class TextFileProcessor 
{ 
public static void textFile() 
{ 
    Scanner keyboard = new Scanner(System.in); 
    System.out.print("Test Input File:"); 
    String inputFile = keyboard.next(); 
    System.out.print("Output File:"); 
    String outputFile = keyboard.next(); 

    try 
    { 
     BufferedReader inputStream = new BufferedReader(new FileReader(inputFile)); 
     PrintWriter outputStream = new PrintWriter(new FileOutputStream(outputFile)); 
     String line = inputStream.readLine(); 

     line = line.replaceAll("\\s+", " ").trim(); 
     line = line.substring(0,1).toUpperCase() + line.substring(1); 
     //This is where I would like to add code 

     while(line != null) 
     { 
      outputStream.println(line); 
      System.out.println(line); 
      line = inputStream.readLine(); 
     } 

     inputStream.close(); 
     outputStream.close(); 
    } 

    catch(FileNotFoundException e) 
    { 
     System.out.println("File" + inputFile + " not found"); 
    } 
    catch(IOException e) 
    { 
     System.out.println("Error reading from file" + inputFile); 
    } 
} 
} 

回答

0

一個簡單的正則表達式就足夠了:

class Ideone 
{ 
    public static void main (String[] args) throws java.lang.Exception 
    { 
     for(String s:"hello? bartender. can I have a drink!whiskey please.".replaceAll("(\\W)(\\s+)", "$1\n").split("\n")) 
      System.out.println(s.substring(0, 1).toUpperCase()+s.substring(1)); 
    } 
} 

輸出:

Hello? 
Bartender. 
Can I have a drink!whiskey please. 

https://ideone.com/Zo2N7Q

+0

它的工作!非常感謝!我現在唯一的問題是新行不以大寫字母開頭。我以爲我解決了這個問題。有任何想法嗎? –

+0

@ nedst3r修復它 – ritesht93

+0

我很欣賞反饋。儘管如此,我無法使用循環來處理我的代碼。我不知道爲什麼。我收到很多錯誤 –

0

這會解決問題了嗎?

if(line.endsWith("! ") || line.endsWith("? ") || line.endsWith(". ")) { 
    line = line + '\n'; 
} 
+0

它會解決問題嗎?由於角色並不總是在行的末尾,所以不會達到OP所要查找的結果。 – kstandell

+0

它沒有工作......但謝謝你的迴應。我什至沒有想到這樣的東西 –

+0

' line = line.replace(「?」,「?\ n」)。replace(「!」,「!\ n」)。replace(「。」,「。 \ n「);'這個版本會在你的行的任何位置插入換行符。 – Selim

0

您可以使用 「捕獲組」 在正則表達式來實現你想要的。

line.replaceAll("(\\?)|(\\!)|(\\.)", "$0\n"); 

更新:

關於如何利用每一行的第一個字符問候你的評論,你可以在Character類使用toUpperCase方法。

line = Character.toUpperCase(line.charAt(0)) + line.substring(1); 

注:

如果您使用的是Java 1.7或以上時,讀取輸入,你應該考慮使用try-與資源塊的Scanner

try (Scanner keyboard = new Scanner(System.in)) { 
    ... 
} 

然後用戶可以在寫入文件之前將其操作爲正確的格式。例如,你可以這樣做......

System.out.println("Enter a line of text:"); 
String[] lines = keyboard.nextLine().replaceAll("(\\?)|(\\!)|(\\.)", "$0\n").split("\n"); 

    // Replace the first character of each line with an uppercase character 
    for (int i = 0; i < lines.length; i++) { 
     lines[i] = Character.toUpperCase(lines[i].charAt(0)) + lines[i].substring(1); 
    } 

    Path path = Paths.get(fileName); 
    Files.write(path, Arrays.asList(lines), Charset.defaultCharset()); 

    System.out.println("This line was written to:" + " " + path.toString()); 
    System.out.println(" "); 

用於讀取和文件,你最好使用在java.nio包非阻塞Files類寫作。它如下簡單:

Path path = Paths.get(fileName); 
Files.write(path, Arrays.asList(lines), Charset.defaultCharset()); 

然後爲了讀取您的文件,您可以使用readAllLines方法。

List<String> lines = Files.readAllLines(path); 

    for (String line : lines) { 
     System.out.println(line); 
    }