• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Beginning code - what am I missing?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 37
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 10
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You declared the age as String, but in your method its int.
 
Shinil Mohan
Ranch Hand
Posts: 37
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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;
     
    What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic