Input related Problem
<p>When we use BufferedReader to take input in one func.
we close it at tat place then we create another obj of BufferedReader in another func of same class, it gives an error...
why??</p>

<p>Code :-</p>

<p>import java.io.*;
public class UseSwitch
{
    private int int_num1,int_num2,int_sum=0;
    private float float_num1,float_num2,float_sum = 0;
    private int ch;</p>

<pre><code>public void intSum()
{
    System.out.println("Enter two integer no.");
            BufferedReader br2 = new BufferedReader(new InputStreamReader (System.in));
    try{
        int_num1 = Integer.parseInt(br2.readLine());
        int_num2 = Integer.parseInt(br2.readLine());
                    br2.close();
    }catch(IOException ex2)
    {
        System.out.println("Please enter correct data");
    }   
    System.out.println("a = " + int_num1 + "\nb = " + int_num2);
    int_sum = int_num1 + int_num2;
    System.out.println("sum of a &amp; b = " + int_sum);
}

public void floatSum()
{
    System.out.println("Enter two floating type no.");
            BufferedReader br1 = new BufferedReader(new InputStreamReader (System.in));
    try{
        float_num1 = Float.parseFloat(br1.readLine());
        float_num2 = Float.parseFloat(br1.readLine());
                    br1.close();
    }catch(IOException ex)
    {
        System.out.println("Please enter correct data");
    }
    System.out.println("a = " + float_num1 + "\nb = " + float_num2);
    float_sum = float_num1 + float_num2;
    System.out.println("sum of a &amp; b = " + float_sum);
}

public void useSwitch()
{
    System.out.println("Enter your choice for sum\n1.Integer values\n2.Float type values");
    try
    {

        ch = Integer.parseInt(br2.readLine());

    System.out.println(ch);
    switch(ch)
    {
        case 1:
            intSum();
            break;
        case 2:
            floatSum();
            break;
        default : 
            System.out.println("Wrong choice");
    }
    br2.close();
    }catch(IOException ex1)
    {
        System.out.println("Please enter correct data");
    }
}
public static void main(String [] args)
{
    UseSwitch us = new UseSwitch();
    us.useSwitch();
}
</code></pre>

<p>}</p>

<p>It gives an error...
while below code is working properly...</p>

<p>*<em>import java.io.</em>;
public class UseSwitch
{
    private int int_num1,int_num2,int_sum=0;
    private float float_num1,float_num2,float_sum = 0;
    private int ch;
    BufferedReader br2 = new BufferedReader(new InputStreamReader (System.in));
    public void intSum()
    {
        System.out.println("Enter two integer no.");
        try{
            int_num1 = Integer.parseInt(br2.readLine());
            int_num2 = Integer.parseInt(br2.readLine());
        }catch(IOException ex2)
        {
            System.out.println("Please enter correct data");
        }<br>
        System.out.println("a = " + int_num1 + "\nb = " + int_num2);
        int_sum = int_num1 + int_num2;
        System.out.println("sum of a &amp; b = " + int_sum);
    }</p>

<pre><code>public void floatSum()
{
    System.out.println("Enter two floating type no.");
    try{
        float_num1 = Float.parseFloat(br2.readLine());
        float_num2 = Float.parseFloat(br2.readLine());
    }catch(IOException ex)
    {
        System.out.println("Please enter correct data");
    }
    System.out.println("a = " + float_num1 + "\nb = " + float_num2);
    float_sum = float_num1 + float_num2;
    System.out.println("sum of a &amp; b = " + float_sum);
}

public void useSwitch()
{
    System.out.println("Enter your choice for sum\n1.Integer values\n2.Float type values");
    try
    {

        ch = Integer.parseInt(br2.readLine());

    System.out.println(ch);
    switch(ch)
    {
        case 1:
            intSum();
            break;
        case 2:
            floatSum();
            break;
        default : 
            System.out.println("Wrong choice");
    }
    br2.close();
    }catch(IOException ex1)
    {
        System.out.println("Please enter correct data");
    }
}
public static void main(String [] args)
{
    UseSwitch us = new UseSwitch();
    us.useSwitch();
}
</code></pre>

<p>}**</p>
