Knowee
Questions
Features
Study Tools

public class Main  {     public static void main(String arg[]) {       Scanner sc=new Scanner(System.in);      //Get the size of array, n, from user     int n=sc.nextInt();      //Declare an array of size 'n'    int arr[]=;   }}

Question

public class Main  {     public static void main(String arg[]) {       Scanner sc=new Scanner(System.in);      //Get the size of array, n, from user     int n=sc.nextInt();      //Declare an array of size 'n'    int arr[]=;   }}

🧐 Not the exact question you are looking for?Go ask a question

Solution

It seems like you're trying to declare an array in Java but you haven't specified the size of the array. Here's how you can do it:

public class Main { 
    public static void main(String arg[]) { 
        Scanner sc=new Scanner(System.in); 
        //Get the size of array, n, from user 
        int n=sc.nextInt(); 
        //Declare an array of size 'n' 
        int arr[]= new int[n]; 
    }
}

In this code, n is the size of the array that you get from the user input. Then, you declare an array arr of size n using new int[n].

This problem has been solved

Similar Questions

import java.util.Scanner;public class Test { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of elements in the array"); int n = sc.nextInt(); //Declare the array int arr[ ]=new int[n]; //Get the elements from user System.out.println("Enter the elements of the array"); for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } //Code to find the sum of odd elements in the array int sum=findSumOddElements();   // invoke the method System.out.println("The sum is "+);        } public int findSumOddElements() { int sum=0; for(int i=0;i<;i++) { if(()!=0) { sum=sum+arr[i]; } }   ;  //return statement }}

for (int i = 0; i < arr.length-1; i++){ for (int j = i+1; j < arr.length; j++) { if( (arr[i].equals(arr[j])) && (i != j) ) { System.out.println(arr[i]); } }}

public class Main { public static void change(int input[]){ input = new int[5]; input[0] = 15; } public static void main(String args[]){ int arr[] = new int[5]; change(arr); System.out.println(arr[0]); }}

Arrange the below code in correct order :Scanner sc=new Scanner(System.in);import java.util.Scanner;int num1 = sc.nextInt();public class Main{}}System.out.println("The value is "+num1);public static void main(String args[]) {

Analyze the following code and choose the correct answer.int[] arr = new int[5];arr = new int[6];The code has compile errors because the variable arr cannot be changed once it is assigned. The code has runtime errors because the variable arr cannot be changed once it is assigned.The code can compile and run fine. The second line assigns a new array to arr.The code has compile errors because we cannot assign a different size array to arr.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.