简体   繁体   中英

how to replace a character in an array java?

I'm doing a function in which I need to get user input and replace all of the vowels in the array with what the user put. This is my array: I honestly dont know what im doing .

char [] letters = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'}

I'm thinking of doing a nested if inside the for loop but like I said I don't know if I'm headed in the right direction .

/********************************************************************************
  This function will prompt the user to replace all vowels in the array
********************************************************************************/
public static void replace( char [] letters ){      
    for(int i =0; i < letters.length; i++){
       if(i >= 'A')
        if(i <='Z')
         System.out.println(letters[i]);
        else
          break;        
    }        
}

Your loop doesn't look like it will do much other than print out all the upper case letters. You need some code to display a prompt and obtain user input. You also need a way to test whether a character is a vowel. One way of doing the latter is:

if ("AEIOU".indexOf(letter) >= 0) {
    // letter is a vowel
}

If you also need to handle lower case letters, you can use either "AEIOUaeiou".indexOf(letter) or "AEIOU".indexOf(Character.toUpperCase(letter)) . This hides the nested loop inside a standard API function call, which makes your code more readable and easier to debug. (You can safely assume that indexOf is written correctly.)

For user interaction, you should look at wrapping System.in with a Scanner . Alternatively (and perhaps better), use a Console for managing all user I/O. Take a look at this tutoral , see if you can figure it out on your own and if you get stuck, post another question.

If I'm interpreting your question correctly, you want to replace a, e, and i with another character that the user has typed in.

  1. You're comparing your index 'i' to characters, you will probably want to change that to
 if(letters[i] >= 'A') if(letters[i] <= 'Z') System.out.println(letters[i]); else break; 

Another thing, if you want to test to see if it is a vowel, you could OR all the comparisons together like this:

 if(letters[i] == 'A' || letters[i] == 'E' || letters[i] == 'I' || letters[i] == 'O' || letters[i] == 'U') letters[i] = users_input 

Maybe something like this?

public static void replace( char [] letters )
{      

    Scanner s = new Scanner(System.in);

    for(int i = 0; i < letters.length; i++)
    {

        if ("AEIOU".indexOf(letters[i]) >= 0)
        {
              System.out.println("\nVowel Found, What should it be replaced with?");
              String line = s.read();
              letter[i] = line.charAt(0);
        }
    }

}

Assume user input is b , you can achieve this:

public static char [] replace(char [] src) {
    String s = new String(src);
    s = s.replaceAll("[aeiouAEIOU]", "b");

    return s.toCharArray();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM