简体   繁体   中英

Unable to run the Java code in Intellij IDEA : error during Compilation

public class RotationsNumbers {
    public int main(String[] args) {
        Solution ans= new Solution();
        int[] arr= {5, 1,2, 3, 4};
        System.out.println(ans.findKRotation(arr , 5));
        return -1;
    }

    class Solution {
        int findKRotation(int arr[], int n) {
            // code here
            int ans= findPivot(arr, 0 , n-1);
            if( ans ==-1) return 0;
            return ans+1;


        }
        int findPivot( int arr[], int start, int end){
            int mid ;
            while(start< end){
                mid= start+ (end- start)/2;
                if (mid < end && arr[mid]> arr[mid+1]){
                    return mid;
                }
                if ( mid > start && arr[mid ]< arr[mid-1]){
                    return mid-1;

                }
                if ( arr[ mid ] < arr[0]){
                    end= mid-1;
                }else{
                    start= mid+1;
                }

            }
            return -1;
        }
    }
}

Edit: Tried using static void main too.. still throwing error For the above code below error is getting thrown, what should i do?

Error: Main method must return a value of type void in class BinarySearch.SortedArray, please define the main method as: public static void main(String[] args)

Process finished with exit code 1

some problems that I see

  1. like mentioned in the comment the main should be public static void main
  2. return statement in main need to go

the below code should work

 public static void main(String[] args) {
        int[] arr= {5, 1,2, 3, 4};
        System.out.println(findKRotation(arr , 5));
    }

    static int findKRotation(int arr[], int n) {
        // code here
        int ans= findPivot(arr, 0 , n-1);
        if( ans ==-1) return 0;
        return ans+1;


    }
    static int findPivot( int arr[], int start, int end){
        int mid ;
        while(start< end){
            mid= start+ (end- start)/2;
            if (mid < end && arr[mid]> arr[mid+1]){
                return mid;
            }
            if ( mid > start && arr[mid ]< arr[mid-1]){
                return mid-1;

            }
            if ( arr[ mid ] < arr[0]){
                end= mid-1;
            }else{
                start= mid+1;
            }

        }
        return -1;
    }

I assume there is a reason you went with the class construction that you've gone with so I won't make any changes beyond addressing your question / concern:

Unable to run the Java code

To run your code make these changes;

Your Solution:

public int main(String[] args) {
        Solution ans= new Solution();
        int[] arr= {5, 1,2, 3, 4};
        System.out.println(ans.findKRotation(arr , 5));
        return -1;
    }

Changed to:

 public static void main(String[] args)
        Solution ans= new Solution();
        int[] arr= {5, 1,2, 3, 4};
        System.out.println(ans.findKRotation(arr , 5));
    }

Your Solution:

class Solution {

Changed to:

static class Solution {

With these minimal changes, your code should run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM