-3

I will compare the alphabet with the string you entered.
I will proceed as follows when the input character string matches the alphabetic character string.
I can not figure out why StringIndexOutOfBoundsException occurs.

Where is the problem?

    b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            int vk_1 = t1.getText().length();

            Set chk = new LinkedHashSet(); 

            for(i=0;i<vk_1;i++) {
                chk.add(t1.getText().toLowerCase().charAt(i)); 
            }

            String result = Arrays.toString(chk.toArray()).replace("[", "").replace(",", "").replace("]", "");

            t2.setText(result.replaceAll("\\p{Z}", "")); 

            if(t1.getText().equals("")) {
                t2.setText("NO KEY");
            }
        }
    });


b2.addActionListener(new ActionListener() {


        public void actionPerformed(ActionEvent e) {

            String s = t2.getText(); // aple
            char p[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
            char c[] = new char[s.length()];
             for(i=0;i<s.length();i++) {
                 for(j=1;j<=26;i++) {
                     if(p[j]==s.charAt(i)) {
                         continue;
                     }
                 }
                 System.out.println(p[j]);
             }
            if(t2.getText().equals("")) t3.setText("no key");
        }       
    });
mina_star
  • 1
  • 1

1 Answers1

2

Java Arrays are Zero based, so your Loop must start at index 0 and run until length-1.

for(j=0;j<26;j++) {

Better way is to use p.length instead of the fix value 26:

for(j=0;j<p.length;j++) {

Second Problem is you use i++ instead of j++

Jens
  • 67,715
  • 15
  • 98
  • 113
  • I have confirmed your updated answer. However, the problem is not solved. I also attach the 't2' text field source code I have input. Please check to see where it is. – mina_star May 31 '17 at 09:35
  • @mina_star please do not Change the question after it is answert. Add new information only marked as Update. – Jens May 31 '17 at 09:41
  • 1
    if the Loop is leaved do not use j anymore, because it will be incremented at the end and j=p.length which leeds to this error. – Jens May 31 '17 at 09:41
  • So, how do I change it? – mina_star May 31 '17 at 09:45
  • @mina_star What should be the output of the line? – Jens May 31 '17 at 09:46
  • t1 text field : input 'apple' t2 text field : 'aple' (duplicate remove) t3 text field : aplefghijkmnoqrstuvwxyzbcd (result) Please refer to the link below for program description. https://stackoverflow.com/questions/44278699/monoalphabetic-substitution-encryption-with-virtual-keys-in-java – mina_star May 31 '17 at 09:50
  • @mina_star I think your print Statement should be inside the Loop. But i can not say it exactly without the Information what it should do – Jens May 31 '17 at 09:51
  • @mina_star Also you again Changed the Content of your question. That makes parts of the anwer obsoletet which is not good for future reader. – Jens May 31 '17 at 09:54
  • The location of the output is inside has complete. The rest of the alphabet except for "aple" is output, but it is repeatedly output as the number of characters as below. bcdefghijkmnoqrstuvwxyz * 4 How do I make it display up to 26 digits? – mina_star May 31 '17 at 10:04
  • @mina_star sorry can you give an very simple example, what is the Input and what should be the Output of your System.out – Jens May 31 '17 at 10:06
  • Input: aple / Output: bcdfghijkmnorstuvwxyz ('aple' Out of left field) – mina_star May 31 '17 at 10:09
  • @mina_star us i understand you Need a new Array, where you store the character you need and print it separatly – Jens May 31 '17 at 10:16