Home > Blockchain >  mute program that I code is not working when I enter a word which ends with m letter
mute program that I code is not working when I enter a word which ends with m letter

Time:10-14

The program have to mute when user input includes "m","a","l" letters side by side. But when I enter a word which ends with "m" word, program doesn't work. I left some pictures about code.

import java.util.Scanner;

public class mute 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        String enteredmessage;

        System.out.print("Enter the message:");
        enteredmessage = scan.nextLine();

        char[] array = enteredmessage.toCharArray();
        mutecommand(array);
        
        scan.close();
    }
    public static void mutecommand(char[] dizi) {
        for (int i = 0; i < dizi.length; i  ) {
            if(dizi[i] == 'm' && dizi[i 1] == 'a' && dizi[i 2]=='l')
            {
                dizi[i]='*';
                dizi[i 1]='*';
                dizi[i 2]='*';
            }
        }

        for (char a : dizi) {
            System.out.print(a);
        }
    }
}

[https://i.stack.imgur.com/tHOFw.png] [https://i.stack.imgur.com/UP32b.png]

CodePudding user response:

It's because of the fact, that you are iterating through the array and if you at the last item and do an i 2 so it's out of the array

  • Related