Search code examples
javaarraysstringbufferedreader

how to accept string array in java using bufferedreader


public static void accept_name( String[] name, int[] r)
{

   InputStreamReader isr = new InputStreamReader(System.in);
   BufferedReader ab = new BufferedReader(isr);
    r = new int[40];
    name = new String[40];
    for(int i=0;i<40;i++)
    {
        System.out.println("Enter the name of students");
        name[i] = ab.readLine();
    }             
}

i am having a problem in name[i] = ab.readLine();

i don't understand what the problem is.


Solution

  • When using Input\Output stream, an IOException is expected, define that it might throw that kind of exception:

    public static void accept_name (String[] name, int[] r) throws IOException
    {
       InputStreamReader isr = new InputStreamReader(System.in);
       BufferedReader ab = new BufferedReader(isr);
    
        r = new int[40];
        name = new String[40];
    
        for(int i=0;i<40;i++)
        {
            System.out.println("Enter the name of students");
            name[i] = ab.readLine();
        }             
    }