Home > Back-end >  I'm unable to store values in array as it is showing error?
I'm unable to store values in array as it is showing error?

Time:04-02

i have to take size of array from user input, and store it and also i tried to store values in array ac to size but when i rum program ,for loop executes once after its showing 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] " ");
}
}

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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 

CodePudding user response:

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...

CodePudding user response:

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.

  • Related