0

ユーザーが入力するすべての文字を印刷したいのですが、私のプログラムはユーザーが入力する最後の値のみを印刷し、最後の値のみが に記録されAscii.txtます。このように見えるはずです

例: ユーザー入力 A、B、c、C

コンマも削除したいのですが、できません:(

「Ascii.txt」の出力は次のようになります。

A = 65
B = 66
c = 99
C = 67

私はまだ学生でプログラミングは初めてなので、私を笑わないでください。どうもありがとう

import java.io.*;

public class NewClass2{
  public static void main(String args[]) throws IOException{



  BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));

  System.out.println("Please Enter  letters separated by comma: ");

  String str = buff.readLine();
  for ( int i = 0; i < str.length(); ++i )
  {
    char c = str.charAt(i);
    int j = (int) c;
    System.out.println(c +" = " + j);
    {
      try
      {
         FileWriter fstream = new FileWriter("Ascii.txt");
         BufferedWriter out = new BufferedWriter(fstream);

         out.write(c+"  =  "+j);
         out.close();

       }catch (Exception e){
       }
     }
    }
  }
  }
4

3 に答える 3

2

FileStream問題は、ASCIIファイルにダンプする文字ごとにを閉じてから再度開くことです。したがって、文字を書き込む前にファイルが空になります。ストリームの作成と終了をループの外に移動するだけです。

    BufferedReader buff = new BufferedReader(new  InputStreamReader(System.in));
    System.out.println("Please Enter  letters separated by comma: ");

    String str = buff.readLine();
    BufferedWriter out = null;
    try
    {
        FileWriter fstream = new FileWriter("Ascii.txt");
        out = new BufferedWriter(fstream);
        for ( int i = 0; i < str.length(); ++i )
        {
            char c = str.charAt(i);
            int j = c;
            System.out.println(c + " = " + j);

            out.write(c + "  =  " + j);

        }
    }

    catch ( Exception e )
    {
        ;
    }
    finally
    {
        if ( out != null )
        {
            out.close();
        }
    }

出力からコンマを削除するには、String.split()を使用することをお勧めします。

    //...          
    String[] splittedStr = str.split(",");
    for ( int i = 0; i < splittedStr.length; i++ )
    {
        if ( splittedStr[i].length() > 0 )
        {
            char c = splittedStr[i].charAt(0);
            int j = c;

            out.write(c + "  =  " + j);
        }
    }
    //...
于 2012-12-18T15:56:54.653 に答える
0

strのループの後でのみファイルに書き込みます。

BufferedWriter out;
try{
FileWriter fstream = new FileWriter("Ascii.txt");
out = new BufferedWriter(fstream);
for ( int i = 0; i < str.length(); ++i ){
  char c = str.charAt(i);
  int j = (int) c;
  System.out.println(c +" = " + j);
  out.write(c+"  =  "+j);
} catch ...{
...
} finally {
   if (out!=null) 
     out.close();
}
于 2012-12-18T15:56:11.750 に答える
0

指摘したように、ファイルに書き込むたびにストリームを閉じています。flush()その方法を探していたのかもしれません。

補足として、Java 7 を使用している場合は、リソースで try を使用できます (そのため、わざわざ閉じる必要はありません)。

BufferedReader buff = new BufferedReader(new  InputStreamReader(System.in));
System.out.println("Please Enter  letters separated by comma: ");

String str = buff.readLine();

try( BufferedWriter out = new BufferedWriter(new FileWriter("Ascii.txt"))){
   //Using split
   String[] splitted = str.split(",");
   for(String s : splitted){
      char c = s.charAt(0);
      int j = c;
      out.write(c + "  =  " + j);
   }
}catch(Exception E){
   //You really should catch specific exceptions here.
}
于 2012-12-18T16:20:38.200 に答える