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
3 Likes
Related Questions
The output of a program which extracts a part of the string "COMPASSION" is as follows:
(a) "PASS"
(b) "PASSION"Write appropriate Java statements to get the above outputs.
Give the output of the following program segment:
for(k=a;k<=a*b;k+=a) { if (k%b==0) break; } System.out.println(k);Give the output when a = 6, b = 4.
Name the type of type casting in the given statements:
(a) double m = 'b';
(b) char ch = (char)68;Write Java statements for the following:
(a) To assign the cube root of -343 to a variable with the appropriate datatype.
(b) To assign the position of the last occurrence of @ in the String s with the appropriate datatype.