0

I am new to Java; can someone please explain to me why this gives me an error

Description Resource Path Location Type Syntax error on token ";", { expected after this token InsertionSort.java /Alghoritems/src/sort line 4 Java Problem

package sort;

public class InsertionSort {
    int[] polje = { -2, 5, -14, 35, 16, 3, 25, -100 };
    
    for(int firstUnsorted = 1; firstUnsorted < polje.length; firstUnsorted++) {
        int newElement = polje[firstUnsorted];
        int i;
        for (i = firstUnsorted; i > 0 && polje[i - 1] > newElement; i--) {
            
        }
    }
    
    for (int i = 0; i < polje.length; i++){
        int firstUnsorted = 1;
        int elemant;
        
    }
    

}
3
  • 3
    Your code is not inside a method Commented Feb 18, 2021 at 9:09
  • 3
    Does this answer your question? Java: Identifier expected Commented Feb 18, 2021 at 9:29
  • Apart from the answers, I would recommend you to learn the basics of the language before doing any sorting. That would really help in the future. Commented Feb 20, 2021 at 9:28

3 Answers 3

1

Each Java application needs an entry point, so the compiler knows where to begin executing the application. In the case of Java applications, you need to wrap up your code in the main() method.

public class HelloWorld{

     public static void main(String []args){
        System.out.println("Hello World");
     }
}

Your code should be

package sort;
    
public class InsertionSort {
    public static void main (String[] args) {
        int[] polje = { -2, 5, -14, 35, 16, 3, 25, -100 };
        
        for(int firstUnsorted = 1; firstUnsorted < polje.length; firstUnsorted++) {
            int newElement = polje[firstUnsorted];
            int i;
            for (i = firstUnsorted; i > 0 && polje[i - 1] > newElement; i--) {
                
            }
        }
        
        for (int i = 0; i < polje.length; i++){
            int firstUnsorted = 1;
            int elemant;
            
        }
        
    }    
}
Sign up to request clarification or add additional context in comments.

Comments

1

Write code in the main method,then the error will not occur.

Comments

0

First of all you have to understand that Java is a programming language that is full on object-oriented. Even when you want to do the simplest thing, you have to create a class with a main method inside. I suggest you start with the basics, before going in for implementations of more difficult algorithms.

package sort;

public class InsertionSort {
   
      public static void main(String[] args)
    {
        int[] polje = { -2, 5, -14, 35, 16, 3, 25, -100 };

        for(int firstUnsorted = 1; firstUnsorted < polje.length; firstUnsorted++) {
            int newElement = polje[firstUnsorted];
            int i;
            for (i = firstUnsorted; i > 0 && polje[i - 1] > newElement; i--) {

            }
        }

        for (int i = 0; i < polje.length; i++){
            int firstUnsorted = 1;
            int elemant;

        }
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.