2017-03-10 8 views
-1

数字のファイル全体を書き直すことを検討しています。 302340372048725280〜3 0 2 3 4 0 3 7 2 0 4 8 7 2 5 2 8 0どうすればいいですか?Java:.txtファイルの "string"をスペース分割で置き換える方法

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Scanner; 

public class IntSeparator { 

    public static void main(String[] args) throws IOException { 
     Scanner scanner; 
     try { 
      scanner = new Scanner(new File("src/extremePuzzles.txt")); 

      PrintWriter spacer = new PrintWriter(new FileWriter("src/extremePuzzles.txt", true)); 

      while (scanner.hasNext()) { 
       String puzzle = scanner.next(); 
       System.out.println(puzzle); 

       String splitted = puzzle.replace("", " ").trim(); 
       System.out.println(splitted); 

       spacer.print(splitted); 
      } 

     } catch (FileNotFoundException e) { 
      System.out.println("file not found!"); 
     } 
    } 
} 

答えて

-1

はい、動作するはずです。それとも、

String splitted = puzzle.replaceAll(".(?=.)", "$0 "); 

を行うことができます基本的には「文字自体は空白が続くとして(最後のものを除く)すべての文字を置き換えます」と言います。 礼儀:Add spaces between the characters of a string in Java?

+0

しかし、それは私の.txtファイルに分割された文字列を書いていません! spacer.print(splitted);は役に立ちません – Tai

+0

thatsはむしろ奇妙です、あなたは車輪を作り直すことができます。シーケンスをループしてスペースを手動で追加し、数が最後の文字に達するまでの間にスペースを追加してみてください – Olagsfark

0

単純な文字列処理は、あなたがあなたの必要性に従って、このコードを使用することができます

public class Str{ 

    public static void main(String args[]){ 
    String str = "302340372048725280"; 

    String temp = ""; 
    int i=0; 
    int len = str.length(); 
    while(len>0){ 
     temp+=str.charAt(i); 
     i++; 
     len--; 
     temp+=" "; 
    } 

    System.out.println(temp); 

    } 
} 

以下のように動作するかもしれません!

関連する問題