Computer Applications

Kamal wants to display only the alphabets stored in a character array. While compiling the code, an error was displayed. Check for the statement with the error and write the correct statement.

char arr[]={'4', '&', 'a', 'w', 'd'}; 
int i; 
for(i=0;i<arr.length;i++) 
{ 
if(Character.isLetter(arr)) 
System.out.println(arr); 
}

Java Library Classes

2 Likes

Answer

char arr[]={'4', '&', 'a', 'w', 'd'}; 
int i; 
for(i=0;i<arr.length;i++) 
{ 
if(Character.isLetter(arr)) //Error 1
System.out.println(arr); // Error 2
}

1. In Character.isLetter(arr), arr is the entire array, but the method expects a single character (char), not an array.

2. In System.out.println(arr), arr prints the whole array, not the individual character.

The corrected code is as follows:

char arr[] = {'4', '&', 'a', 'w', 'd'}; 
int i; 
for(i = 0; i < arr.length; i++) 
{ 
    if(Character.isLetter(arr[i]))
        System.out.println(arr[i]);
}

Answered By

1 Like


Related Questions