Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

question code of java

2 views
Skip to first unread message

mak

unread,
Apr 13, 2007, 7:35:19 AM4/13/07
to
hi, i need your help, i wrote the below code in java and i hava some
problems
The compiler tells me :
class interface or enum expected
the code is
the mistake appears to the line import java.io.*;

public class Array{
int[] T;
int n;
/*constructor*/
public Array(int size ){
T=new int[size];
n=size;

}
/*add element into array*/
public void insert(int position,int elem){
T[position]=elem;

}
/*find minimum algorithm*/
public int findmin(){
int[] resultsArray=new int[2];
int min=T[0],index=0;
for (int i=1;i<n;i++){
if (T[i]<min){
min=T[i];
index=i;

}
}
resultsArray[0]=min;
resultsArray[1]=index;
return resultsArray;

}
}//end class
import java.io.*;
public class Findminimumapp{
/*====main method============*/
public static void main(String[] args) throws IOException {

int n=0;
int min=0;
int index=0;
String str=null;
/*construct new array object*/
System.out.print("give array size then press enter");
str=Utils.getString();
n=Integer.parseInt(str);
Array arrayObj=new Array(n);
/*read array*/
System.out.println("type"+n+"integers in separate lines");
System.out.println("==============");
for(int i=0;i<n;i++){
str=Utils.getString();
arrayObj.insert(i,Integer.parseInt(str));

}
//*printresults*/
min=arrayObj.findMin()[0];
index=arrayObj.findMin()[1];
System.out.println("minimum element in array is"+min);
System.out.println("minimum position is"+(index+1));
}//end main method
}//end class

Tom Hawtin

unread,
Apr 13, 2007, 8:16:55 AM4/13/07
to
mak wrote:
> The compiler tells me :
> class interface or enum expected
> the code is
> the mistake appears to the line import java.io.*;
>
> public class Array{
> [...]

> }//end class
> import java.io.*;
> public class Findminimumapp{

Are you putting both classes in the same file? Public classes need to be
in a file that matches the class name. So you should have two files:
Array.java and Findminimumapp.java.

Actually, you should use initial caps for class names, so
FindMinimumApp.java. In general you should keep to coding standard -
including indentation, spaces and capitalisation. This is particularly
true if anyone else is ever going to read your code, or you ever read
anyone else's code. Oh and use meaningful variable names, and all the
usual stuff.

Tom Hawtin

Patricia Shanahan

unread,
Apr 13, 2007, 8:59:38 AM4/13/07
to
mak wrote:
> hi, i need your help, i wrote the below code in java and i hava some
> problems
> The compiler tells me :
> class interface or enum expected
> the code is
> the mistake appears to the line import java.io.*;
>
> public class Array{
...

> }//end class
> import java.io.*;
> public class Findminimumapp{
...

imports apply to the file, not the class, and must be grouped at the
start of the file.

"class interface or enum" are the things that can be declared at the top
level, and so could immediately follow the end a top level class
declaration.

Most Java compilers require each public class to be in a file with the
matching name.

In general, if you have multiple classes that are sufficiently unrelated
that they need different import lists, even if only one of them is
public, consider putting them in separate files. You can still group
them together relative to the rest of the program by putting them in a
package.

Patricia

Roedy Green

unread,
Apr 20, 2007, 8:50:47 AM4/20/07
to
On 13 Apr 2007 04:35:19 -0700, "mak" <kert...@yahoo.gr> wrote,
quoted or indirectly quoted someone who said :

>class interface or enum expected

see
http://mindprod.com/jgloss/compileerrormessages.html#CLASSDCLEXPECTED
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

0 new messages