Click to See Complete Forum and Search --> : Array (Urgent)


f_boboy
May 30th, 2001, 08:56 AM
Hi, I have a problem with my array program
My code:


import java.io.*;
import java.text.*;


public class Q1a
{
public static void main (String[] args) throws IOException
{
int numb1;
int tempsum;

BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));

System.out.print("How many numbers will you enter?");
numb1 = Integer.parseInt(stdin.readLine());

System.out.println ("Enter " + numb1 + " integers one per line:");
int [] list = new int[numb1];
for(int index=0; index < list.length; index++)
{
System.out.flush();
list[index]=Integer.parseInt(stdin.readLine());
}

tempsum = sumArray(list);
System.out.println ("The Sum is " + tempsum);

for(int index=0; index < list.length;index++)
{
System.out.flush();
System.out.println ((list[index] + percentage(list[index],tempsum)) + "% of the sum.");
}
}
}

public int sumArray(int[] temp1)
{
int sum=0;

for(int index=0; index < temp1.length; index++)
{
sum = temp1[index] + sum;

}
return sum;
}

public float percentage(float a, float b)
{
float c=0.0f;
float x,y
x = a;
y = b;
c = (x/y) * 100;

return c;
}




The error:

35:Identifier expected.
public int sumArray(int[] temp1)
^
35: 'class' or 'interface' keyword expected.
public int sumArray(int[] temp1)
^
24: Method sumArray(int[]) not found in class Q1a.
tempsum = sumArray(list);
^
30: Method percentage(int, int) not found in class Q1a.
System.out.println ((list[index] + percentage(list[index],tempsum)) + "% of the sum.");


Can anybody help me whats wrong with my code, thanx

bayard
May 30th, 2001, 09:03 AM
Your java code is structured improperly.

Notice the class delcaration you have at the top, the { ... } that link to it should go around all your methods.
Currently you have one method inside it and two methods outside.

The compiler hits your 2nd method (sumArray..) and thinks it should be either another class or an interface.

Bayard
bayard@generationjava.com

Brainbench MVP for Java
http://www.brainbench.com

Javaboy
May 30th, 2001, 12:23 PM
Just move the bracket above the line:

public int sumArray(int[] temp1)

to the very bottom of the page.

Hope this helps,

Javaboy

Paolo Mazzoni
May 30th, 2001, 05:13 PM
I saw the answers you received and they're good, infact you must include your functions (or better methods) in a class definition because Java is an object oriented programming language and you can define only classes or interfaces, it's not like c++ with wich you can define public functions that doesn't belong to any class!


Il Paolo