File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Beginning code - what am I missing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginning code - what am I missing?" Watch "Beginning code - what am I missing?" New topic
Author

Beginning code - what am I missing?

Justin Bruns
Greenhorn

Joined: Sep 09, 2011
Posts: 6
I'm doing a self-taught course and I was given some mangled code to fix up. When i try to run it in eclipse I get the error "firstName cannot be resolved to a variable" among other things. What am I missing?
Justin Bruns
Greenhorn

Joined: Sep 09, 2011
Posts: 6
I thought these lines
# //begin method to set last name
# public void setLastName; String last;

were supposed to look like
# //begin method to set last name
# public void setLastName) String last;

but the Eclipse error log seemed to think otherwise... I feel like I've made a knot of this and I just need a push int he right direction. Can anyone suggest where I should be looking for my problem? I appreciate your time!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11160
    
  16

You have other problems...or at least, I do. What is that ';' doing in the middle of line 15? What is that '.' doing on line 52? Also, you cannot declare a method inside a method - which is what it looks like you are doing on line 15. You start your main method, then immediately say "begin method to set first name". Methods cannot be nested like that. The main method has to be closed before you try and start any other one.

It is really just about impossible to help you if we can't see the actual code you are trying to compile.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11160
    
  16

update - my guess is that Eclipse is confused by the nested methods, so it can't REALLY tell you what the problem is. It's making a guess that is totally wrong.

Move your method around so they are not nested, and try again.

Finally, they should look like this:

Justin Bruns
Greenhorn

Joined: Sep 09, 2011
Posts: 6
Thank you! I've tried to fix my grammar and closed the main method before the rest of the code, now it looks like this, is that the appropriate locationf or my public static void main line?:



The error messages I get are a string of errors saying "firstName cannot be resolved to a variable, lastName cannot be resolved to a variable, years cannot be resolved to a variable" and so on. Thanks for your help!
Shinil Mohan
Ranch Hand

Joined: Aug 24, 2011
Posts: 37

Here you are missing the variable declarations, something like below. You can add this to the code



Also check your getAge() method, this is not returning age, its returning years.
Justin Bruns
Greenhorn

Joined: Sep 09, 2011
Posts: 6
I'm screwing something up regarding v ariable input, I think. I get the following five errors:
Description Resource Path Location Type
first cannot be resolved to a variable Person.java /Person line 27 Java Problem
last cannot be resolved to a variable Person.java /Person line 40 Java Problem
age cannot be resolved to a variable Person.java /Person line 53 Java Problem
years cannot be resolved to a variable Person.java /Person line 59 Java Problem
age cannot be resolved to a variable Person.java /Person line 69 Java Problem

and my code now looks like this:



I keep going through the chapter that this exercise is from and I can't seem to figure out what I'm missing... the nested method comment earlier helped put me back on track, and now I just need to figure out this variable situation. Thanks!
Samuel Fitzpatrick
Greenhorn

Joined: Jan 23, 2003
Posts: 10

Some of the issues I see are:
* your Person class does not have the three variables declared
* your constructor has something in the signature that does not belong
* your constructor is also named wrong.. case matters

Also, not an issue, but an improvement
* you should use your set methods in the Person constructor
Samuel Fitzpatrick
Greenhorn

Joined: Jan 23, 2003
Posts: 10

in your methods, your arguments need to match the code within the method..

you have



the method is trying to set your class variable to "last", which does not exist. You need to change your argument from "String lastName" to "String last"
Shinil Mohan
Ranch Hand

Joined: Aug 24, 2011
Posts: 37



Also in the newly posted code you have used the same name for setting and getting the property - getFirstName(). It's better to use different method names as you have used earlier [getFirstName() and setFirstName()].
Justin Bruns
Greenhorn

Joined: Sep 09, 2011
Posts: 6
Thanks to you guys' awesome notes, I have it down to the three error messages:
Description Resource Path Location Type
age cannot be resolved to a variable Person.java /Person line 53 Java Problem
age cannot be resolved to a variable Person.java /Person line 59 Java Problem
age cannot be resolved to a variable Person.java /Person line 67 Java Problem


There's something I'm not getting about the way that age and year variable is going to display.
Shinil Mohan
Ranch Hand

Joined: Aug 24, 2011
Posts: 37

You have to declare the age as well as you did for lastName and firstName.



Also check your getAge() method, it should return age.
Samuel Fitzpatrick
Greenhorn

Joined: Jan 23, 2003
Posts: 10

You still need to declare one more class variable. You may want to do these in one place, its hard to see that you only declared two of the three.
Also constructors do not have a return type, so you cannot put one in your signature.
Justin Bruns
Greenhorn

Joined: Sep 09, 2011
Posts: 6
Awesome, you guys are badass. I declared the age variable, I believe, fixed the constructor signifier, and now I get these three error messages:
Description Resource Path Location Type
Type mismatch: cannot convert from int to String Person.java /Person line 66 Java Problem
Type mismatch: cannot convert from String to int Person.java /Person line 72 Java Problem
Type mismatch: cannot convert from int to String Person.java /Person line 80 Java Problem



} // end class Person
Shinil Mohan
Ranch Hand

Joined: Aug 24, 2011
Posts: 37

You declared the age as String, but in your method its int.
Shinil Mohan
Ranch Hand

Joined: Aug 24, 2011
Posts: 37

Also check if you really need the below method years()



you already have setAge()



This is the same case with firstName() and lastName(). This method do the same job as setFirstName() and setLastName()
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
Justin Bruns wrote:I'm doing a self-taught course and I was given some mangled code to fix up. . . .
And boy, have they mangled it.

What you haven't been told yet:
  • 1: A lot of people dislike // end comments. I myself think they are useful, but that is controversial.
  • 2: But I have never seen // begin comments. The comments should be before the class/method and should be surrounded by /** ...*/. Those are documentation comments.
  • So, delete all the begin comments, as well as inane comments like that in
    import java.util.Scanner; // uses Scanner
    Whoever wrote that knows Java, and has introduced compiler errors into their code. The structure of every method is there, but you need to work out the errors. Do not try to change the structure of any of the methods, at all. If it says. . . in fact the order of things and the names of things are all correct. People have already told you to get an instance field (it's not called a class field) firstName, and remember the spelling must be exactly the same as in the method to the left of the =. So you add a firstName field to the class, not labelling it static. Then you find out the syntax for methods and you work out where the () go and where the , and where the ; go. And get rid of some of the unnecessary comments.
    You work out whether there are any spelling errors like firstname/firstName.
    You work out whether there are any { or } out of place; you have already noticed the } for the main method is out of place.
    Another thing haven't been told is that the if block ought to have { } in, so put { } and newlines and indentation after the if in the setAge method.

    Note, there are no logic errors in that class. It will compile and run if you don't create any methods. You simply need to repair the misspellings, add the three fields, and then you can put something into the main method which will show it working.
    A class should have its equals() hashCode() and toString() methods overridden, but I think that is beyond the scope of the test they have given you.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38016
        
      22
    Earlier, I wrote:. . . Another thing haven't been told is that the if block ought to have { } in, so put { } and newlines and indentation after the if in the setAge method. . . .
    . . . and the {} go around age = years;
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: Beginning code - what am I missing?
     
    Similar Threads
    Simple Person Program
    Error setting the expression with value [Ljava.lang.String;@1d05b0a
    Comparable/Comparator query
    Query about compareTo() method
    Collection of values question