Hi ! Lunik. I have seen your program and made a few changes to it.
Java Program :
import java.io.*;
public class array_names
{
public static void main()throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter The First Alphabet : ");
char a = in.readLine().charAt(0),c;
String[] n = new String[10];
for(int i=0;i<10;i++)
{
System.out.print("\nEnter The Name One By One : ");
n[i]=in.readLine();
}
System.out.println("\nThe Name(s) Whose First Alphabet Starts With "+a+" are : ");
for(int j=0;j<10;j++)
{
c=n[j].charAt(0);
if(c==a)
System.out.println(n[j]+" ");
}
}
}
See, I have removed the String " s " and found two mistakes.
1) The first mistake was with the Buffer part. Your buffer " in " was probably not initialised before the loop started, so the first cell of your array went blank, generating an IndexOutofBounds error when you went for checking.
2) Secondly, your character " c " needed to be initialised before the loop "j".
3) Extra : I have further beautified your program. Please check and comment.
Java Program :
import java.io.*;
public class array_names
{
public static void main()throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter The First Alphabet : ");
char a = in.readLine().charAt(0),c;
String[] n = new String[10];
for(int i=0;i<10;i++)
{
System.out.print("\nEnter The Name One By One : ");
n[i]=in.readLine();
}
System.out.println("\nThe Name(s) Whose First Alphabet Starts With "+a+" are : ");
for(int j=0;j<10;j++)
{
c=n[j].charAt(0);
if(c==a)
System.out.println(n[j]+" ");
}
}
}
See, I have removed the String " s " and found two mistakes.
1) The first mistake was with the Buffer part. Your buffer " in " was probably not initialised before the loop started, so the first cell of your array went blank, generating an IndexOutofBounds error when you went for checking.
2) Secondly, your character " c " needed to be initialised before the loop "j".
3) Extra : I have further beautified your program. Please check and comment.
What's This Plz Tell Me Somthing About This.............I want To Learn Java and i am joing java closs from april...............
ReplyDelete