Results 1 to 6 of 6
  1. #1
    jnanasri is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Question Need guidance in java arrays

    Hi ,

    I got strucked at 19 th line during compilation in below program.Please help me in getting it compiled.It was showing message like .class file expected.


    public class Array1{

    int j=0,i=0;
    int arr[]=new array[20];

    public void fact(int n,int arr[])
    {
    arr[j]=1;
    while(arr[j]<=40)
    {
    n*arr[j]=arr[i];
    j++;
    }
    System.out.println(arr[i]);
    }
    public static void main(String []args){
    int n=4;
    int arr[]= new arr[20];
    Array1 a = new Array1();
    a.fact(n,arr[]); // [error .class file expected]
    System.out.println("End of program");
    }
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,767
    Rep Power
    5

    Default Re: Need guidance in java arrays

    Change:

    Java Code:
    int arr[]= new arr[20];
    
    // to
    
    int arr[]= new int[20];
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jnanasri is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: Need guidance in java arrays

    But still it is not compiling

    public class Array1{

    public static int arr[] = new int[20];
    public static int n=4;
    public static void fact(int n,int arr[])
    {
    int j=0,i=0;
    arr[j]=1;
    while(arr[j]<=40)
    {
    n*arr[j]=arr[i];
    j++;
    }
    System.out.println(arr[i]);
    }
    public static void main(String []args){

    fact(n,arr[]);
    }
    }

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,767
    Rep Power
    5

    Default Re: Need guidance in java arrays

    Please place your code between code tags for formatting.

    Java Code:
    public class Array1 {
    
       public static int arr[] = new int[20];
       public static int n     = 4;
    
       public static void fact(int n,int arr[])
       {
           int j=0,i=0;
           arr[j]=1;
           while(arr[j]<=40)
           {
               n*arr[j]=arr[i];  // syntactically wrong
               j++;
           }
           System.out.println(arr[i]);
       }
    
       public static void main(String []args){
           fact(n,arr[]); // problem is here - just use arr
       }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jnanasri is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: Need guidance in java arrays

    Im learner still ...
    Still it is showing ArrayIndexOutOfBounds Exception is there anything left to correct.Please help me

    public class Array{

    public static void fact(int n,int arr[],int arr1[])
    {
    int j=0,i=0;
    arr1[j]=1;
    while(arr[i]<=40)
    {
    arr[i]=n*arr1[j];
    j++;
    arr1[j]=arr1[j]+1;
    }
    System.out.println(arr[i]);
    }
    public static void main(String []args) throws Exception{
    int n=4;
    int arr[] = new int[40];
    int arr1[]=new int[20];
    fact(n,arr,arr1);
    }
    }

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,767
    Rep Power
    5

    Default Re: Need guidance in java arrays

    You need to put in some print statements to see what the values of i and j are as the loops iterate. Other print statements may also be useful. And you also need to include the full error message which usually provides information about where the error occurred and in this case, what caused the exception.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Need guidance, first Java assignment.
    By Creature in forum New To Java
    Replies: 5
    Last Post: 10-02-2013, 05:04 AM
  2. Replies: 3
    Last Post: 12-12-2012, 08:48 PM
  3. Looking for guidance
    By Syphax in forum New To Java
    Replies: 3
    Last Post: 05-30-2012, 08:09 AM
  4. Replies: 2
    Last Post: 01-19-2012, 07:43 PM
  5. Java assignment, need guidance ...
    By trejorchest in forum New To Java
    Replies: 5
    Last Post: 04-17-2011, 05:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •