class Arrayd
{
static int max(int x[]){
int i,max;
max=x[0];
for(i=0;i<x.length;i++)
{
if(x[i]>max)
{
x[i]=max;
}
}
return(max);
}
}
public static void main(String... s)
{
int k=0;
System.out.println("\n...Calling a function to print the maximum no. occuring in an array...");
k=Arrayd.max(new int[]{10,20,30,40,50});
System.out.println("Maximum value=");
System .out.println(k);
}
Everytime i compile this program in the terminal it gives me:
class,interface or enum expected.
Please help me with this error!
View Answers
August 31, 2012 at 1:26 PM
You have written main method outside the class. Put the main method inside the class. Also, your code displays 10 as the maximum number which is wrong, output should be 50.So we have modified your code.Check it:
class Arrayd
{
static int max(int x[]){
int largest=x[0];
for(int i=1;i < x.length;i++){
if(x[i] > largest){
largest = x[i];
}
}
return largest;
}
public static void main(String... s)
{
int k=0;
System.out.println("\n...Calling a function to print the maximum no. occuring in an array...");
k=Arrayd.max(new int[]{10,20,30,40,50});
System.out.println("Maximum value=");
System .out.println(k);
}
}
Related Tutorials/Questions & Answers:Advertisements
core java
core java Hi,
can any one expain me serialization,Deseralization and exterenalization in core javacore java
core java i need core java material
Hello Friend,
Please visit the following link:ADS_TO_REPLACE_1
Core Java
ThanksCORE JAVA
CORE JAVA What is Garbage collection in java? What is the role of a developer for garbage collectioncore java
core java In java primitive variables will get its default value automatically after declaration. Then why it is mandatory to initialize a variable before usingCore Java
Core Java Please write a Java Program to design login form and store the values in file & validate and display the MainFormCore Java
Core Java Hi,
Can any one please share the code for Binary search in java without using builtin functioncore java
core java Hello sir,What is logic behinde the core java programms,How may programmas are there,for example,sorting of two numbers,grade of the student details,fibonice serice,paldroma,incremting of the program,assceddingcore java
core java Hello sir,What is logic behinde the core java programms,How may programmas are there,for example,sorting of two numbers,grade of the student details,fibonice serice,paldroma,incremting of the program,assceddingCore Java
Core Java How to load class dynamically in java ?
To load class dynamically you can use Class class method
Class.forName("abc.xyz.MyClass");
This method load the given class at run timeCore Java
Core Java Hi,
can any one please tell me the uses of return type,"Super" and "this" calling statement in Java??
why do we required this,super calling statement??
why return type is requiredCore Java
Core Java Hi,
Can any one please expain me why derived data types are required in java as we have primitive data types with usCore Java
Core Java Hi,
Can any one please expain me why derived data types are required in java as we have primitive data types with usCore Java
Core Java How can i write own compile time and runtime exceptions in java
Hello Friend,
Please visit the following links:
http://www.roseindia.net/java/exceptions/how-to-throw-exceptions.shtml
httpCore java
Core java How to use hyperlink that is href tag in core java without swing, frames etc.
My code is
StringBuffer oBodyStringBuffer = new StringBuffer("Message Classification: Restricted.\n\nCore Java
Core Java Is Java supports Multiple Inheritance? Then How ?
Hi Friend,
Java does not support multiple inheritance but it can be achieved by using the interface.ADS_TO_REPLACE_1
In Java, Multiple Inheritance cancore java
core java I am having 10 countries in the data base as back end.in front end i am having a button in web page like SHOWCOUNTRIESLIST so i need java code how to retrive that 10 countries from back end to my page,,pls help meCore Java
Core Java Hi,
Can any one please tell me the program to print the below matrix in a spiral order.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Thanks a lat in advancecore java
core java how to write or update into excel file using...; Please visit the following links:
http://www.roseindia.net/tutorial/java/poi/insertIntoExcelFileData.html
http://www.roseindia.net/answers/viewqa/JavaCore Java
Core Java can any one please tell me the difference between Static and dynamic loading in java???
The static class loading is done through the new operator while dynamic class loading is achieved through Run timeCore Java
Core Java What is the significance of static synchronized method?
Why do we have the method declared as static synchronizedcore java
core java how to compare every character in one string with every character in other stringCore Java
Core Java Write a Program to add given number of days to the current system date and display the sameCore Java
Core Java have to find the prime numbers which is less than the current prime numbers using loopscore java
core java can i use native keyword with abstract method ? if yes explain and if no please explaincore java
core java Is it possible to create a shallow copy of an arraylist instance? if yes then please post the codeCore Java
Core Java Is Java supports Multiple Inheritance? Then How ?
There is typo the question is ,
What is Marker Interface and where it can be used?
Hi Friend,ADS_TO_REPLACE_1
An interface with no methodcore java
core java what does the term web container means exactly?please also give some examples
Hi,
In Java Platform, Enterprise Edition specification, servlet container comes into picture. It is also know as web containercore java
core java java program using transient variable
Hi Friend,
A transient variable is a variable that may not be serialized.The transient..., visit the following link:
http://www.roseindia.net/help/java/t/transient-javacore java - Java Beginners
Core Java interview Help Core Java interview questions with answers Hi friend,Read for more information.http://roseindia.net/interviewquestionsJAVA(core) - Java Beginners
JAVA(core) Core Java In java 'null' is keyword which means object have nothing to store. even not allocated memory