简体   繁体   中英

I'm unable to store values in array as it is showing error?

I have to take the size of the array from user input and store it and also I tried to store values in array ac to size but when I run the program, for loop executes once after its showing an error of boundary exception

 import java.util.Scanner;     

 public class Odd {
    int size;
    int a[] = new int[size];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Size of array");
        size = sc.nextInt();
        for (int i = 0; i < size; i++) {
            System.out.println("enter " + i + "th element");
            a[i] = sc.nextInt();
        }
        System.out.println("your array is");
        for (int i = 0; i < size; i++) {
            System.out.print(a[i] + " ");
        }
    }
}

When you write

public class Odd
{
int size;
int a[]=new int[size];

then whenever Odd is constructed, a is created to be an array with size equal to the current value of size , which is 0.

When you then write

size=sc.nextInt();

the size of a does not change to the new value of the size variable.

There are several things wrong with your code.

First, the static method main is not able to access size and a because they are instance variables. You would need to make them static or make them local variables in the main method.

Second, size is zero. You create the array without knowing what the user will enter for size. The array has a size of zero.

Third, you don't need the variable size since you don't really use it anywhere else.

import java.util.Scanner;
public class Main{

   public static void main(String[]args){
      Scanner sc=new Scanner(System.in);
   
      System.out.println("Enter Size of array");
      int[] a = new int[sc.nextInt()];
   
      for(int i=0;i<a.length;i++){
         System.out.println("enter "+i+"th element");
         a[i]=sc.nextInt();
      }
   
      System.out.println("your array is");
      for(int i=0;i<a.length;i++){
         System.out.print(a[i]+" ");
      }
      
   }
}

Move the array initialization to inside main method as the size is required while creating array.

public class Odd {
    static int size;
    static int a[];
    public static void main(String[]args){
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Size of array");
        size=sc.nextInt();
        a = new int[size];
        for(int i=0;i<size;i++)
        {
            System.out.println("enter "+i+"th element");
            a[i]=sc.nextInt();
        }
        System.out.println("your array is");
        for(int i=0;i<size;i++)
        {
            System.out.print(a[i]+" ");
        }

    }
}

and here is the output

Enter Size of array
3
enter 0th element
1
enter 1th element
2
enter 2th element
3
your array is
1 2 3 

When the array is created, the value of size is defaultly 0. Do this for better results:

public class Odd
{
    int size;
    int a[];
    public static void main(String[]args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Size of array");
        size=sc.nextInt();
        a = new int[size];

Then whatever you have to do you can do...

You don't actually need these member variables.

import java.util.Scanner;

public class SizeArray {
    public static void main(String[]args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Size of array: ");
        int size = sc.nextInt();
        int a[] = new int[size];

        for(int i = 0; i < size;i ++) {
            System.out.print("enter "+ i +"th element: ");
            a[i]=sc.nextInt();
        }

        System.out.println("your array is");

        for(int i = 0; i < size; i++) {
            System.out.print(a[i]+" ");
        }

        sc.close();
    }
}

You can just put in the main function instead of modifying them after creation.

Output:

Enter Size of array: 4
enter 0th element: 1
enter 1th element: 2
enter 2th element: 3
enter 3th element: 4
your array is
1 2 3 4

BTW please write your code in more readable format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM