# Thread: Need help regarding Arrays Help pls....

1. Member
Join Date
Jan 2009
Posts
19
Rep Power
0

## 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? ");
System.out.println("Enter values.");
for(int i : mainArr)
{
}}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. Member
Join Date
Oct 2008
Location
UK
Posts
65
Rep Power
0
remove the square brackets:

Java Code:
`maxNo = findMax(mainArr);`

3. Member
Join Date
Jan 2009
Posts
19
Rep Power
0
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? ");
System.out.println("Enter values.");
for(int i : mainArr)
{
}}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. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
9
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.

#### Posting Permissions

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