Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: i'm getting th error, when i put a method to get the position of the smallest no

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question i'm getting th error, when i put a method to get the position of the smallest no


     public class   SmallestIndex {
     
     
    	//This method returns the index of the smallest value in the array of given size
         private static int arrSize = 0;
     
     
    	public static void  smallestIndex (int[] array) {
     
     
    		int currentValue = array[0];
     
    		int smallestIndex = 0;
     
    		for (int j=1; j < arrSize; j++) {
    			if (array[j] < array[smallestIndex])
    				smallestIndex = j;
    		}
    		System.out.println();
    		System.out.println("The smallest index is: "+ smallestIndex);
     
    	}
    	}
     
     
     


    it compiles correctly but when run program this error come
    --------------------Configuration: my - JDK version 1.6.0_27 <Default> - <Default>--------------------
    java.lang.NoSuchMethodError: main
    Exception in thread "main"
    Process completed.

    so to correct this when i put the static void main(String[]args) so many other errors come again!!!


    pls help me correcting this... your members help is greatly appreciated..

    thank you!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: i'm getting th error, when i put a method to get the position of the smallest n

    so many other errors come again!!!
    You need to post the full text of the errors you get so we can help you solve the problem.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: i'm getting th error, when i put a method to get the position of the smallest n

    so to correct this when i put the static void main(String[]args) so many other errors come again!!!

     
        public class SmallestIndex {
           public static void main(String[]args){
        static final int LINESIZE = 10;
        static final int MAXSIZE = 50;
        static final int MINVALUE = Integer.MIN_VALUE;
        private static int arrSize = 0;
     
        //This method returns the index of the smallest value in the array of given size
        public static void smallestIndex (int[] array) {
        int currentValue = array[0];
        int smallestIndex = 0;
        for (int j=1; j < arrSize; j++) {
        if (array[j] < currentValue)
        currentValue = array[j];
        }
        System.out.println();
        System.out.println("The smallest index is: "+ currentValue);
        }
     
        public static void printArray(int[] array) {
        System.out.println("The numbers in the array are: ");
        for (int j=0; j< arrSize; j++) {
        if (j%LINESIZE == 0 )
        System.out.println();
        System.out.printf("%5d", array[j]);
        }
        }
     
     
       }
    }


    i get the errors 16

    illegal start of expression - 8 errors
    ';' expected - 5 errors
    '.class' expected - 2 errors

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: i'm getting th error, when i put a method to get the position of the smallest n

    JUST HELP ME IN THIS CODE!!!!

    I WANT TO FIND THE INDEX OF THE SMALLEST VALUE IN THIS .....

     
     
     public class   SmallestIndex {
     
     
    	//This method returns the index of the smallest value in the array of given size
         private static int arrSize = 0;
     
     
    	public static void  smallestIndex (int[] array) {
     
     
    		int currentValue = array[0];
     
    		int smallestIndex = 0;
     
    		for (int j=1; j < arrSize; j++) {
    			if (array[j] < array[smallestIndex])
    				smallestIndex = j;
    		}
    		System.out.println();
    		System.out.println("The smallest index is: "+ smallestIndex);
     
    	}
    	}
    Last edited by izzahmed; November 6th, 2011 at 12:06 AM.

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: i'm getting th error, when i put a method to get the position of the smallest n

    Quick question: Why is it that you have arrSize = 0 and you make j < arrSize? Wouldn't the for loop only run once then, if at all?

    *edit* I guess it's to initialize the variable but won't it just stay 0?

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: i'm getting th error, when i put a method to get the position of the smallest n

    OH MY GOD!!!!!!

    just correct me this,,, include or exclude....

    I WANT TO GET THE INDEX OF LARGEST ELEMENT IN AN ARRAY

    ----------------------------------------------------------------------------------------------------------[
     
     
    class small {
      // method to display the contents of an array
      static void displayData(int[] data)   {
        for (int index=0; index != data.length; index++)
          System.out.println(data[index]+"\t");
      }
     
      static int positionOfLargest(int[] data, int limit)   {
        // method to return the position of the largest item
        // in the data with bounds 0..limit
     
        int largest = data[0];
        int indexOfLargest = 0;
     
        for (int index=1; index <= limit; index++)  {
          if (data[index]> largest)  {
            largest = data[index];
            indexOfLargest = index;
          }
        }
        return indexOfLargest;
      }
     
     
     
      public static void main(String[] args)   {
        int[] data = {18,7,15,8,13};
     
        System.out.println("numbers\n");
        displayData(data);
     
        System.out.println("Index of Largest\");
       indexOfLarget(data);
     
      }
    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: i'm getting th error, when i put a method to get the position of the smallest n

    Does it work now? If not please show the problem and explain.

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: i'm getting th error, when i put a method to get the position of the smallest n

    Quote Originally Posted by izzahmed View Post
    so to correct this when i put the static void main(String[]args) so many other errors come again!!!

     
        public class SmallestIndex {
           public static void main(String[]args){
        static final int LINESIZE = 10;
        static final int MAXSIZE = 50;
        static final int MINVALUE = Integer.MIN_VALUE;
        private static int arrSize = 0;
     
        //This method returns the index of the smallest value in the array of given size
        public static void smallestIndex (int[] array) {
        int currentValue = array[0];
        int smallestIndex = 0;
        for (int j=1; j < arrSize; j++) {
        if (array[j] < currentValue)
        currentValue = array[j];
        }
        System.out.println();
        System.out.println("The smallest index is: "+ currentValue);
        }
     
        public static void printArray(int[] array) {
        System.out.println("The numbers in the array are: ");
        for (int j=0; j< arrSize; j++) {
        if (j%LINESIZE == 0 )
        System.out.println();
        System.out.printf("%5d", array[j]);
        }
        }
     
     
       }
    }


    i get the errors 16

    illegal start of expression - 8 errors
    ';' expected - 5 errors
    '.class' expected - 2 errors
    You cannot have "inner methods" in java. It looks like you have a method inside the main method.

Similar Threads

  1. Replies: 1
    Last Post: November 5th, 2011, 10:56 AM
  2. method to return the position of the smallest item in an array
    By izzahmed in forum Java Theory & Questions
    Replies: 5
    Last Post: November 4th, 2011, 04:18 AM
  3. method undefined error
    By Herah in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 08:38 PM
  4. error: This method must return a result of type int
    By J05HYYY in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2011, 04:26 PM
  5. where is the error in this method !
    By M7MD in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 9th, 2010, 05:08 PM