Results 1 to 4 of 4
  1. #1
    shaggyoo7 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default Need help regarding Arrays Help pls....

    This is my under develope code i tried to compile it and i m getting an error
    --------------------------------------------------------------------------
    import java.io.*;
    class NumberSorting
    {
    private int [] mainArr,primeArr,oddArr,evenArr;
    int mainArr[] = new int[50];
    int primeArr[] = new int[50];
    int oddArr[] = new int[50];
    int evenArr[] = new int[50];
    private int primeAvg, oddAvg, evenAvg, size;
    public void create()
    {
    try{
    System.out.println("How many no. of elements do you want to add in the array? ");
    BufferedReader a = new BufferedReader(new InputStreamReader(System.in));
    size = Integer.parseInt(a.readLine());
    System.out.println("Enter values.");
    for(int i : mainArr)
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    mainArr[i] = Integer.parseInt(br.readLine());
    }}catch(Exception e){System.out.println(e);}
    }
    private int findMax(int []mainArr)
    {
    int maxno = mainArr[0];
    for(int i : mainArr)
    {
    if(mainArr[i]>maxNo)
    {
    maxNo = mainArr[i];
    }
    }
    return maxNo;
    }
    public void seperate()
    {
    int maxNo, select = 0, j, pa = 0, oa = 0, ea = 0;
    maxNo = findMax(mainArr[]);
    for(int i : mainArr)
    {
    for(j = 2 ; j <= maxNo ; j++)
    {
    if(mainArr[i]%j == 0)
    {select = 0;
    break;}
    }
    if(j == mainArr[i])
    {primeArr[pa] = mainArr[i];
    pa++;}
    }
    for(int i : mainArr)
    {
    if(mainArr[i]%2 == 0)
    {evenArr[ea] = mainArr[i];
    ea++;}
    else if(mainArr[i]%2 ==1)
    {oddArr[oa] = mainArr[i];
    oa++;}
    }
    }
    }
    --------------------------------------------------------------------------
    The error i m getting is
    --------------------------------------------------------------------------
    D:\Program Files\Java\jdk1.7.0\bin>javac NumberSorting.java
    NumberSorting.java:38: '.class' expected
    maxNo = findMax(mainArr[]);
    ^
    1 error
    --------------------------------------------------------------------------
    Pls help needed

  2. #2
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    remove the square brackets:

    Java Code:
    maxNo = findMax(mainArr);

  3. #3
    shaggyoo7 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Question

    Hey i did the necessary changes but now there is a problem in further developed code I get a NumberFormatException
    Have a look
    --------------------------------------------------------------------------
    import java.io.*;
    class NumberSorting
    {
    int pa = 0, oa = 0, ea = 0;
    private int mainArr[] = new int[50];
    int primeArr[] = new int[50];
    int oddArr[] = new int[50];
    int evenArr[] = new int[50];
    private int primeAvg, oddAvg, evenAvg, size;

    public void create()
    {
    try{
    System.out.println("How many no. of elements do you want to add in the array? ");
    BufferedReader a = new BufferedReader(new InputStreamReader(System.in));
    size = Integer.parseInt(a.readLine());
    System.out.println("Enter values.");
    for(int i : mainArr)
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    mainArr[i] = Integer.parseInt(br.readLine());
    }}catch(Exception e){System.out.println(e);}
    }
    private int findMax(int []mainArr)
    {
    try{
    int maxNo = mainArr[0];
    for(int i : mainArr)
    {
    if(mainArr[i]>maxNo)
    {
    maxNo = mainArr[i];
    }
    }
    return maxNo;
    }catch(Exception e){System.out.println(e);return 0;}
    }
    public void seperate()
    {
    try{
    int maxNo, select = 0, j;
    maxNo = findMax(mainArr);
    for(int i : mainArr)
    {
    for(j = 2 ; j <= maxNo ; j++)
    {
    if(mainArr[i]%j == 0)
    {select = 0;
    break;}
    }
    if(j == mainArr[i])
    {primeArr[pa] = mainArr[i];
    pa++;}
    }
    for(int i : mainArr)
    {

    if(mainArr[i]%2 == 0)
    {evenArr[ea] = mainArr[i];
    ea++;}
    else if(mainArr[i]%2 ==1)
    {oddArr[oa] = mainArr[i];
    oa++;}
    }}catch(Exception e){System.out.println(e);}
    }
    static public double average(int []arr,int len)
    {
    try{
    double ans = 0;
    for(int i : arr)
    {
    ans += (double)arr[i];
    }
    ans /=len;
    return ans;
    }catch(Exception e){System.out.println(e);return 0.0;}
    }


    }
    --------------------------------------------------------------------------
    class NumAvg
    {
    public static void main(String args[])
    {
    try{
    NumberSorting n1 = new NumberSorting();
    n1.create();
    n1.seperate();
    double primeAverage = NumberSorting.average(n1.primeArr, n1.pa);
    System.out.println("The Prime numbers average is "+primeAverage);
    }catch(Exception e){System.out.println(e);}
    }}
    --------------------------------------------------------------------------
    When i run the code i get the output as follows
    --------------------------------------------------------------------------
    D:\Program Files\Java\jdk1.7.0\bin>java NumAvg
    How many no. of elements do you want to add in the array?
    10
    Enter values.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    java.lang.NumberFormatException: For input string: ""
    The Prime numbers average is NaN

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    you enter empty string by enter(?), which cant be a number, and throw NumberFormatException...

    i would like to ask... you get the "size", and no matter what the size is, users must enter 50 numbers?
    and your program not support double

    i think you should use "size" input in the for loop, and check user input is valid or not
    Last edited by mtyoung; 01-14-2009 at 05:39 AM.

Similar Threads

  1. Help on Arrays...
    By cuellar14 in forum New To Java
    Replies: 4
    Last Post: 07-25-2008, 09:16 PM
  2. need help with arrays
    By Jman in forum New To Java
    Replies: 17
    Last Post: 07-21-2008, 03:34 AM
  3. Arrays
    By bunbun in forum New To Java
    Replies: 1
    Last Post: 04-09-2008, 03:24 AM
  4. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 03:45 PM
  5. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 11:08 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
  •