-3

The below code is not working as after providing the size integer there is an error displayed.

Error

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at ARR.Number(Arr.java:13)
at ARR1.main(Arr.java:42)' 

Java Program

import java.io.BufferedReader;
import java.io.IOException;

class ARR
{
public static BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
int arr[];
void Number (int n) throws IOException 
{
    for (int i=0; i<n; i++)
    {
        arr[i]= Integer.parseInt(br1.readLine());
    }
}
void display(int n)
{
    for (int i=0; i<n; i++)
    {
        System.out.print(arr[i]+"\t");
    }
}
void Search(int n,int num)
{
    for (int i=0; i<n; i++)
    {
        if (arr[i]== num)
        {
            System.out.println("Number Found");
        }
    }
}
}
class ARR1
{
public static void main (String[] args) throws IOException
{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    ARR obj = new ARR();
    System.out.println("Enter No size: " );
    int a = Integer.parseInt(br.readLine());
    obj.Number(a);
    obj.display(a);
    System.out.println();
    System.out.println("Enter the Number to search:");
    int b= Integer.parseInt(br.readLine());
    obj.Search(a,b);
}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Abhishek
  • 1
  • 2
  • You tried to parse non-numeric data as an integer, this is the problem. – Tim Biegeleisen Nov 18 '16 at 03:52
  • `For input string: ""` there isn't a number there. That is an issue, and you haven't told the computer what to do if there is no number there. That would be the problem. – ifly6 Nov 18 '16 at 03:52
  • @Tim Biegeleisen and@ifly6: After providing the size of the arrray say 5.. I have to store the data in the array. As soon as i give a number say 5I am getting the error.. Please help me how to modify my code – Abhishek Nov 18 '16 at 04:15
  • @TimBiegeleisen: After providing the size of the arrray say 5.. I have to store the data in the array. As soon as i give a number say 5I am getting the error.. Please help me how to modify my code – Abhishek Nov 18 '16 at 04:22

2 Answers2

0

I got the answer to this.. The Problem was the array object was not created.. Below is the code which helped me out.

void Number (int n) throws IOException 
{
    **arr=new int[n];**
    for (int i=0; i<n; i++)
    {
        arr[i]= Integer.parseInt(br1.readLine());
    }
}
Abhishek
  • 1
  • 2
0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class ARR {

    private int arrSize;
    private int[] arr;
    private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    void Number(int n) throws IOException {
        arrSize = n;
        arr = new int[arrSize];
        System.out.println("Enter numbers you want to input");
        for (int i = 1; i <= n; i++) {
            System.out.print(i+": ");
            try {
                arr[i-1] = Integer.parseInt(br.readLine());
            } catch (NumberFormatException ne) {
                i--;
                System.out.println("Error. Please Input a Number!");
            }
        }
    }

    void display(int n) {
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + "\t");
        }
    }

    void Search(int n, int num) {
        boolean isNumberFound = false;
        for (int i = 0; i < n; i++) {
            if (arr[i] == num) {
                isNumberFound = true;
                break;
            } 
        }

        if(isNumberFound) {
            System.out.println("Number Found");
        } else {
            System.out.println("Not on the List");
        }



    }
}

public class ARR1 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        ARR obj = new ARR();

        int a = 0;
        boolean isWrong = true;
        while (isWrong) {
            try {
                System.out.print("Enter No size: ");
                a = Integer.parseInt(br.readLine());
                isWrong = false;
            } catch (NumberFormatException ne) {
                System.out.println("Error. Please Input a Number!");
            }
        }
        obj.Number(a);
        obj.display(a);
        System.out.println();


        isWrong = true;
        int b = 0;
        while (isWrong) {
            try {
                System.out.println("Enter the Number to search:");
                b = Integer.parseInt(br.readLine());
                isWrong = false;
            } catch (NumberFormatException ne) {
                System.out.println("Error. Please Input a Number!");
            }
        }
        obj.Search(a, b);
    }
}

I edit your code. I used some Exception Handling to avoid the error when you input inappropriate inputs. study exception handling for you will know how to use the exception you are encountering. It could be an advantage to your codes later on.

see this link.

Exception Handling

msagala25
  • 1,806
  • 2
  • 17
  • 24