-1

Want to convert JtextArea text to bits String.

I'm using two JTextArea one for input and one for output and one button which is executing following code:

StringBuilder sb = new StringBuilder();

int ssc =Integer.parseInt(jta1.getText());
                        String sc=Integer.toBinaryString(ssc);
                        char[] bc=sc.toCharArray();
                        for (char c : bc) {
                        sb.append("-");


int i =Character.getNumericValue(c);
                    String a = String.valueOf(i);
                    sb.append(a.toString());
                    jta2.setText(a);
Error           

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "dsa"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
        at java.lang.Integer.parseInt(Integer.java:449)
        at java.lang.Integer.parseInt(Integer.java:499)
        at MainFrame$2.actionPerformed(MainFrame.java:57)
  • 1
    Where is the issue? Can you explain it a bit more? – Braj May 25 '14 at 13:07
  • This is the error im getting when i click button Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "dsa" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:449) at java.lang.Integer.parseInt(Integer.java:499) at MainFrame$2.actionPerformed(MainFrame.java:57) – programmer one May 25 '14 at 13:16
  • Its clear from the exception that it is converting a string into integer. – Braj May 25 '14 at 13:23
  • Read it here [Restricting JTextField input to Integers](http://stackoverflow.com/questions/11093326/restricting-jtextfield-input-to-integers) – Braj May 25 '14 at 13:24

1 Answers1

0

Integer.parseInt(jta1.getText());

Assuming you are trying to parse more than just numbers, the above is causing the NumberFormatException. the parser can only parse number strings like "1234", not letters "dsa".

java.lang.NumberFormatException: For input string: "dsa"

What you can do is iterate through the characters and just cast each to an int and append the binary string of each character

for (char c : txt.toCharArrray() ) {
    String binaryOfCharacter = Integer.toBinaryString((int)c);
    // append to StringBuilder
}

Note: each character only returns the seven bit repsresentation

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720