2013-04-24 2 views
0

Ok 그래서 여기에 내 프로그램이 있지만 작동하지만 다음 줄로 이동하는 데 문제가 있습니다. 대신 길어지면 화면에서 사라집니다.바이너리 프로그램에 텍스트에 대한 새 줄 만들기

import javax.swing.*; 

public class TextToBinary{ 
public static void main(String[] args) { 

    String y; 

    JOptionPane.showMessageDialog(null, "Welcome to the Binary Canary.","Binary Canary", JOptionPane.PLAIN_MESSAGE); 

    y = JOptionPane.showInputDialog(null,"Write the word to be converted to binary: ","Binary Canary", JOptionPane.PLAIN_MESSAGE); 

    String s = y; 
     byte[] bytes = s.getBytes(); 
     StringBuilder binary = new StringBuilder(); 
     for (byte b : bytes) 
     { 
     int val = b; 
     for (int i = 0; i < 8; i++) 
     { 
      binary.append((val & 128) == 0 ? 0 : 1); 
      val <<= 1; 
     } 
     binary.append(' '); 
     } 

     JOptionPane.showMessageDialog(null, "'" + s + "' to binary: " +"\n" +binary,"Binary Canary", JOptionPane.PLAIN_MESSAGE); 

    } 
} 
+0

System.getProperty ("line.separator"); –

+0

여기 좀보세요 http://stackoverflow.com/questions/7696347/to-break-a-message-in-two-or-more-lines-in-joptionpane –

답변

0

이미 8 진수 그룹을 만들어 바이트를 만듭니다. 한 행에 표시 할 바이트 수에 대한 카운터를 추가하기 만하면됩니다.

이 예제를 코드화하기 위해 4 개를 골랐습니다.

import javax.swing.JOptionPane; 

public class TextToBinary { 
    public static void main(String[] args) { 

     String y; 

     JOptionPane.showMessageDialog(null, "Welcome to the Binary Canary.", 
       "Binary Canary", JOptionPane.PLAIN_MESSAGE); 

     y = JOptionPane.showInputDialog(null, 
       "Write the word to be converted to binary: ", "Binary Canary", 
       JOptionPane.PLAIN_MESSAGE); 

     String s = y; 
     byte[] bytes = s.getBytes(); 
     StringBuilder binary = new StringBuilder(); 
     int byteCount = 0; 
     for (byte b : bytes) { 
      int val = b; 
      for (int i = 0; i < 8; i++) { 
       binary.append((val & 128) == 0 ? 0 : 1); 
       val <<= 1; 
      } 
      binary.append(' '); 
      byteCount++; 
      if (byteCount >= 4) { 
       binary.append("\n"); 
       byteCount = 0; 
      } 
     } 

     JOptionPane.showMessageDialog(null, "'" + s + "' to binary: " + "\n" 
       + binary, "Binary Canary", JOptionPane.PLAIN_MESSAGE); 

     System.exit(0); 
    } 
} 
관련 문제