提供大小输入后,Java程序会出错

时间:2016-11-18 03:51:02

标签: java arrays

以下代码无效,因为提供大小整数后会显示错误。

错误

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程序

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);
}
}

2 个答案:

答案 0 :(得分:0)

我得到了答案..问题是没有创建数组对象..下面的代码帮助了我。

void Number (int n) throws IOException 
{
    **arr=new int[n];**
    for (int i=0; i<n; i++)
    {
        arr[i]= Integer.parseInt(br1.readLine());
    }
}

答案 1 :(得分: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);
    }
}

我编辑你的代码。当您输入不适当的输入时,我使用了一些Exception Handling来避免错误。为您学习异常处理将知道如何使用您遇到的异常。以后这可能对您的代码有利。

请看这个链接。

Exception Handling