Error: error: class, interface, or Enum expected

Asked

Viewed 4,794 times

0

Why is my code making the mistake:

error: class, interface, or Enum expected

Follows the code:

import java.util.Scanner;

    public class teste175 {

        public static void main (String[] args) {

            Scanner sc = new Scanner (System.in);

            int a, i;
            a = sc.nextInt();

            int v[] = new int [a];

            for (i=0; i<a; i++) {
                v[i] = sc.nextInt();
            }
            funcao (v, a);
        }
    }

    static int funcao (int v[], int a){

        int temp, j, i;

        for (j=0; j<a; j++){
            for (i=1; i<a; i++){

                if (v[i]<v[i-1] {
                    temp = v[i];
                    v[i] = v[i-1];
                    v[i-1] = temp;
                }
            }
        }
        return v;
    }
}
  • 1

    A tip, add the indented code, makes it easier to read it if the problem is simple. And another, it is no use to throw the code here and wait for someone to find out where the error occurred, besides the code, preferably indented, always point out where the error occurs in the code.

1 answer

2


One of the main reasons you’re always struggling to understand what’s going on in the code is their lack of organization. Looking at this code really is very difficult to find an error there, even for very experienced programmers. Organizing it makes it very easy to find all errors present in the code, not only reported. This goes for all your code, in Java, C++ or other language.

The function is outside the class, parentheses are missing, there is wrong return, just to mention some problems. So it is much simpler and easier to read:

import java.util.Scanner;

class HelloWorld {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int[] v = new int[a];
        for (int i = 0; i < a; i++) v[i] = sc.nextInt();
        funcao(v);
        for (int i = 0; i < a; i++) System.out.println(v[i]);
    }

    static void funcao(int[] v) {
        for (int j = 1; j < v.length; j++) {
            for (int i = 1; i < v.length; i++) {
                if (v[i] < v[i - 1]) {
                    int temp = v[i];
                    v[i] = v[i - 1];
                    v[i - 1] = temp;
                }
            }
        }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Nor will I get into the merit of the algorithm being good or bad, I do not know the goal.

  • I use the gedit of Ubuntu to write the codes, you know any tool that helps me in this sense in the linux itself? thanks since

  • 1

    No editor is going to organize your code, that’s what you have to do. There’s a lot of editor or IDE that can help a little bit. The goal here is not to recommend anything, but if you look for Eclipse, Netbeans, Code::Blocks will find something a little better. Some people don’t want to install all this and use other things, like the Sublime.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.