2017-02-15 12 views
0

Scannerの文字列を入力し、バイナリ形式のASCII番号に変換し、各文字の頻度を出力するプログラムを作成しました。我々はすべて、各ASCIIが長い7ビットの長さであるべきであるが、それは先行ゼロをキャッチされていないとして、私のプログラムのために、それは必ずしもそうではありません知っているように、それはJavaでアスキー文字の欠けている先行ゼロをキャッチする方法

import java.util.*; 

public class ASCII 
{ 
    public static void main (String [] args) 
    { 
     Scanner sc = new Scanner (System.in); 
     System.out.println("Enter your sentence: "); 
     String myString = sc.nextLine(); 
     int length = myString.length(); 
     int ascii; 
     char character; 
     int[] myarray = new int[256]; 
     for(int i =0; i < length; i++) 
     { 
      character = myString.charAt(i); 
      ascii = (int)character; // casting the character into an int 
      System.out.print(Integer.toBinaryString(ascii) + " "); 
      myarray[ascii]++; // counting the frequency of each letter in the string 
     } 

     System.out.println(); 

     for (int k = 0; k < myarray.length; k++) 
     { 
      if (myarray[k] > 0) // if the frequency is greater than 0 do this print line, without this programm would print all 255 ascii characters. 
      { 
       System.out.println("'"+(char)k + "'" + " appeared " + myarray[k]  + " times."); 
      } 
     } 
    } 
} 

あるとしてここに は私のプログラムです。私は、文字列を渡していた場合 たとえば、「こんにちは」 予想される出力は

"1101000 1100101 1101100 1101111 0100000 1110100 1101000 1100101 1110010 1110010 1100101" 

する必要がありますが、私は取得しています:

"1101000 1100101 1101100 1101100 1101111 100000 1110100 1101000 1100101 1110010 1100101" 

あなたが「100000」を見ることができるようにだけです6ビットの長さと私のアスキー値のいくつかは、この先行ゼロの問題のために間違っている

私はこの問題を修正する方法についてはわからないので、私はここにいるのです。誰かが私にこれをどのように修正できるかを示すことができれば、それは素晴らしいことだろう。

+0

使用しているときに起こっていること:System.out.print(Integer.toString(ascii、2)+ ""); –

+0

はい、0〜127の番号の128のASCIIコードポイントがあり、0〜127の値としてエンコードされています。しかし、JavaがASCIIを使用するという前提に基づいて、あなたのプログラムにはいくつかの矛盾があります。 Java、JavaScript、.NET、XML、HTML、... [Unicode](http://www.unicode.org/charts/nameslist/index.html)を使用してください。 'charAt(i)'は、UTF-16コード単位を返します。その1つまたは2つは、Aまたは€やUnicodeなどのUnicodeコードポイントをエンコードします。 C0 ControlsやBasic Latinブロックなどのサブセットのみを処理する場合は、プログラムにガード(if-throwまたは類似)を入れる必要があります。 –

答えて

-1
public static void main (String [] args) 
{ 
    String myString = "hello there"; 
    int length = myString.length(); 
    int ascii; 
    char character; 
    int[] myarray = new int[256]; 
    for(int i =0; i < length; i++) 
    { 
     character = myString.charAt(i); 
     ascii = character; // casting the character into an int 
     // not optimal, but works 
     System.out.println(String.format("%7s", Integer.toBinaryString(ascii)).replace(' ', '0')); 

     myarray[ascii]++; // counting the frequency of each letter in the string 

    } 
    System.out.println(); 

    for (int k = 0; k < myarray.length; k++) 
    { 
     if (myarray[k] > 0) // if the frequency is greater than 0 do this print line, without this programm would print all 255 ascii characters. 
     { 
      System.out.println("'"+(char)k + "'" + " appeared " + myarray[k]  + " times."); 
     } 
    } 
} 
+0

このアプローチを使用すると、7文字ではなく1文字あたり8ビットが実際に印刷されるので、7を印刷できるようになりますか? –

+0

もちろん、ここに入力してください。訂正ありがとう – kamehl23

0

あなたはint型に文字列を変換停止し、直接文字列を比較し、または文字列にKを変換する必要がありますいずれかが、その後、文字に変換し、先行ゼロのに必要な量を追加します。

関連する問題