• Post Reply Bookmark Topic Watch Topic
  • New Topic

error: class, interface, or enum expected  RSS feed

 
Tom Lindsey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, could someone explain to me why I am receiving these errors? I have two other classes, if that is relevant.

Here are the errors:



Here is the code:



I am unsure that I follow.

Here are my other two classes:

Contact.java



This one gives me these errors:



SortPersonList.java:

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if that is ALL your code, then no, it won't work.

Java requires all methods to be in a class. You can't just write a stand alone method.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Common causes for that sort of error:-
You might have spelt class wrongly, classs or Class by mistake. (Or Public, etc)
You might have code or a { before the beginning of the class.
You might not have balanced your {} and have too many } at the end of the class.
 
Tom Lindsey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:if that is ALL your code, then no, it won't work.

Java requires all methods to be in a class. You can't just write a stand alone method.


What about with these other classes? That works, right? I thought that the goal was to have these methods divided into classes that would reference each other so that the work is divided amongst them.
 
Tom Lindsey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Common causes for that sort of error:-
You might have spelt class wrongly, classs or Class by mistake. (Or Public, etc)
You might have code or a { before the beginning of the class.
You might not have balanced your {} and have too many } at the end of the class.


I can't find any odd number of brackets, but thank you for teaching me.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Lindsey wrote:What about with these other classes? That works, right? I thought that the goal was to have these methods divided into classes that would reference each other so that the work is divided amongst them.

Yes, but based on what you've shown, your quickSort method isn't in a class. That would also explain the error message. The compiler is expecting the first declaration to be a class declaration, so as soon as it hits "static void" it gets very confused.

in your Contact class, the error is that socialsecuritynumber is declared as an int. Which is a primitive type, so you can't call any methods on it. But then you try to. That's what "int cannot be dereferenced" means - you can't treat a primitive as if it was an object reference.

And welcome to the Ranch!
 
Tom Lindsey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Tom Lindsey wrote:What about with these other classes? That works, right? I thought that the goal was to have these methods divided into classes that would reference each other so that the work is divided amongst them.

Yes, but based on what you've shown, your quickSort method isn't in a class. That would also explain the error message. The compiler is expecting the first declaration to be a class declaration, so as soon as it hits "static void" it gets very confused.

in your Contact class, the error is that socialsecuritynumber is declared as an int. Which is a primitive type, so you can't call any methods on it. But then you try to. That's what "int cannot be dereferenced" means - you can't treat a primitive as if it was an object reference.

And welcome to the Ranch!


Thank you very much, good to be here!

Should I just forget about declaring socialsecuritynumber and number as ints, and instead declare them as strings? I figured that the quick sort would not work if they are strings. I thought that they must be numbers if they were to be compared.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Lindsey wrote:Should I just forget about declaring socialsecuritynumber and number as ints, and instead declare them as strings?

I would, but not because this won't work otherwise.

If you're using int, you just need to use primitive comparisons. So use == instead of equals(), and < instead of compareTo(). Using equals() is correct for any non-primitive (and it's a common mistake to do the opposite of what you're doing, and use == when you should use equals()).

However, I generally use numeric types only when I have things that are actually going to be treated like numbers. So, for instance, I want to perform arithmetic on them. Otherwise I'd use Strings, even if all the characters were digits. Just to give one quick example why - if you have a number that starts with a 0, then storing it as a number drops the 0.

You can still compare Strings - they implement Comparable so you can use compareTo (your code would actually be correct using Strings). You do need to remember that comparisons are now alphabetical, not numerical, though. This makes no difference if the lengths are always the same, but if they aren't you have to remember that "9" will be considered greater that "19999999", for instance.

Another possibility is to create SocialSecurityNumber and ZipCode classes to represent the values. You're still going to need a String (or int) internally, but that is then a detail hidden from the rest of the program. And it allows you to add features such as validation more easily (making sure that only valid lengths are used, for instance).
 
Tom Lindsey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I have made some changes, and I seem to have made some headway.

First I have Contact.java which now gives me no errors!



Next I have SortPersonList.java which only gives me three errors! (So excited.)



And finally I have Sorting.java that only gives me two errors!



The errors are:

 
Tom Lindsey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Tom Lindsey wrote:Should I just forget about declaring socialsecuritynumber and number as ints, and instead declare them as strings?

I would, but not because this won't work otherwise.

If you're using int, you just need to use primitive comparisons. So use == instead of equals(), and < instead of compareTo(). Using equals() is correct for any non-primitive (and it's a common mistake to do the opposite of what you're doing, and use == when you should use equals()).

However, I generally use numeric types only when I have things that are actually going to be treated like numbers. So, for instance, I want to perform arithmetic on them. Otherwise I'd use Strings, even if all the characters were digits. Just to give one quick example why - if you have a number that starts with a 0, then storing it as a number drops the 0.

You can still compare Strings - they implement Comparable so you can use compareTo (your code would actually be correct using Strings). You do need to remember that comparisons are now alphabetical, not numerical, though. This makes no difference if the lengths are always the same, but if they aren't you have to remember that "9" will be considered greater that "19999999", for instance.



Oh, OK, I will correct myself then, thank you.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorting.java:26: error: cannot find symbol
swap(data, left, right);
^


You seem to have forgotten to write a swap method.
 
Tom Lindsey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realize now that I should have put in words exactly what it is I am trying to accomplish.

I want to create an array of contact names (firstName, lastName, socialsecuritynumber, and zipcode). Then I would like to sort them based on the socialsecuritynumber by using the quicksort method.
 
Tom Lindsey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Sorting.java:26: error: cannot find symbol
swap(data, left, right);
^


You seem to have forgotten to write a swap method.


Oh.

Ohhhhhh.

 
Tom Lindsey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Lindsey wrote:
Matthew Brown wrote:Sorting.java:26: error: cannot find symbol
swap(data, left, right);
^


You seem to have forgotten to write a swap method.


Oh.

Ohhhhhh.



Added that
Compiled everything
1 error

Weeping tears of joy.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Lindsey wrote:
1 error

Weeping tears of joy.



And what does that very detailed error message tell you?
 
Tom Lindsey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Tom Lindsey wrote:
1 error

Weeping tears of joy.



And what does that very detailed error message tell you?


I am too embarrassed to ask.
 
Tom Lindsey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Lindsey wrote:
Jeff Verdegan wrote:
Tom Lindsey wrote:
1 error

Weeping tears of joy.



And what does that very detailed error message tell you?


I am too embarrassed to ask.



Yeah, I am not getting this.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Lindsey wrote:Yeah, I am not getting this.

What does your quicksort() method look like, and what have you written on line 13? The error message is very explicit, and explains exactly what's wrong.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Lindsey wrote:
Jeff Verdegan wrote:


And what does that very detailed error message tell you?


I am too embarrassed to ask.


SortPersonList.java:13: error: method quickSort in class Sorting cannot be applied to given types;
On line 13 of SortPersonList.java, you're calling quickSort(), but the types of the arguments you're passing don't match the types of the arguments for any version of that method. If method foo() expects a String, you can't call foo(123), because 123 is an int, not a String.

Sorting.quickSort(persons);
In particular, you're calling quicksort(persons). So whatever type persons is, it doesn't match what quicksort() was expecting.

required: Comparable[],int,int
The method requires you to pass (Comparable[], int, int). Those things have some meaning to the method that it needs in order to do its job.

found: Contact[]
What you actually passed was an array of Contact. If Contact implements Comparable, then that's fine for the first argument, but you still need to provide the two ints.

reason: actual and formal argument lists differ in length
The number of parameters you provided (1--the Contact[]) is not the same as the same as the number of parameters the method expected you to give it (3--the Comparable[] and the two ints).
 
Tom Lindsey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Tom Lindsey wrote:
Jeff Verdegan wrote:


And what does that very detailed error message tell you?


I am too embarrassed to ask.


SortPersonList.java:13: error: method quickSort in class Sorting cannot be applied to given types;
On line 13 of SortPersonList.java, you're calling quickSort(), but the types of the arguments you're passing don't match the types of the arguments for any version of that method. If method foo() expects a String, you can't call foo(123), because 123 is an int, not a String.

Sorting.quickSort(persons);
In particular, you're calling quicksort(persons). So whatever type persons is, it doesn't match what quicksort() was expecting.

required: Comparable[],int,int
The method requires you to pass (Comparable[], int, int). Those things have some meaning to the method that it needs in order to do its job.

found: Contact[]
What you actually passed was an array of Contact. If Contact implements Comparable, then that's fine for the first argument, but you still need to provide the two ints.

reason: actual and formal argument lists differ in length
The number of parameters you provided (1--the Contact[]) is not the same as the same as the number of parameters the method expected you to give it (3--the Comparable[] and the two ints).


I cannot figure out how to fix that.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Lindsey wrote:I cannot figure out how to fix that.

Tom, it sounds like you simply want someone to do your work for you - and that's not going to happen.
You need to ShowSomeEffort (←click).

Stop saying you "can't", and look at the definition of your quickSort() method again - carefully.
Then look at the call that is causing the problem, and finally: read the error message thoroughly.

People have gone out of their way to explain every facet of the problem to you. If you still can't work out what the matter is with all the information you've been given, then maybe programming isn't for you.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!