Search code examples
javajoptionpane

Concatenate characters to a string - java


In Joptionpane my characters are shown one by one in the dialog message. I need them to form a word , as for the example: if I use Sytem.out.println(c) it will show them in a line. I want to show them in a line together in joptionpane too

for (int i = 0; i < encrypt.length(); i++) {
        char c = encrypt.charAt(i);
        if (Character.isLetter(c)) {
            c -= shift;
            if(c < 'A'){
                c = (char) (((int) c + (int) ('A')) % 26 + (int) ('A'));
            }else{
                c = (char) (((int) c - (int) ('A')) % 26 + (int) ('A'));
            }

                            JOptionPane.showMessageDialog(null, c);
        }

    }

Solution

  • String result = "";
    for (int i = 0; i < encrypt.length(); i++) {
            char c = encrypt.charAt(i);
            if (Character.isLetter(c)) {
                c -= shift;
                if(c < 'A'){
                    c = (char) (((int) c + (int) ('A')) % 26 + (int) ('A'));
                }else{
                    c = (char) (((int) c - (int) ('A')) % 26 + (int) ('A'));
                }
    
    
            }
    result += String.valueOf(c);
        }
    JOptionPane.showMessageDialog(null, result);