2016-04-14 4 views
2

入力ファイルを変更するプログラムを作成しています。それは新しい行を開始する必要があります? 。そして!しかし、私はそれを把握していないようです。新しい行はまた、私が持っていると思う大文字で始める必要があります。それはまた私が持っていると信じている不必要なスペースもなくすはずです。期間、疑問符、および感嘆符の後に新しい行を挿入するにはどうすればよいですか?

例:hello?バーテンダー。私は飲み物をもらえますか?ウィスキーしてください。

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); 

注:

あなたがからの入力を読んだとき、あなたが次にScanner

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

ためのtry-と、リソースブロックを使用することを検討すべきであるのJava 1.7以上を使用している場合ユーザーはファイルに書き込む前に正しい形式に操作することができます。たとえば、あなたが何かなどを行うことができます...ファイルからの読み込みと書き込みのためとして

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); 
    } 
関連する問題