Jump to content

Need help declaring a variable

Go to solution Solved by PC-NOOB,

Put your if statement inside the for loop. 

I got a problem with my code. The word "Entry" in the Addressbook is for some reason not working. It has red lines underneath it. What should i do?
 
Here's the code:
     class Person{

     private int age;
     private String firstname;
     private String lastname;
     private String middlename;
    
     public Person(){
     age = 0;
     firstname = "";
     lastname ="";
     middlename ="";
     
     }
     public Person(String first, String middle, String last){
     this.age=age;
     this.firstname= first;
     this.middlename=middle;
     this.lastname=last;    
     }
     
     public String getfirstname(){
     return firstname;    
     }
     public String getmiddlename(){
     return middlename;    
     }
     public String getlastname(){
     return lastname;    
     }
     
     public void setfirstname(String F){
     firstname = F;    
     }
     public void setmiddlename(String I){
     middlename = I;    
     }
     public void setlastname(String L){
     lastname = L;  
     }
     public void setage(int age){
     this.age = age;
     } 
     
     public String getfullname(){
     return firstname+" "+middlename+" "+lastname;    
     }   
     public int getage(){
     return this.age;    
     }         
     }
     class Addressbook{
     private Person[] entries;
     private int numPeople;
     public Addressbook(){
     numPeople = 0;
     entries = new Person [150];
     }
     public Addressbook(Person[]people){
     numPeople = people.length;
     entries = people;
     }
     public int getsize(){
     return numPeople;
     }
     public Person getPerson(int position){
     return entries[position];    
     }
     public Person getEntry(int index){
     return entries [index];    
     }
     public int addEntry(Person added){
     entries[numPeople] = added;
     numPeople++;
     return numPeople;
     }
     public Addressbook filterbyage(int lower, int upper){
     Addressbook found; 
     found = new Addressbook();
     for(int i=0; i<numPeople;i++){
     Person Entry;
     Entry = this.getEntry(i);
     }
     if(Entry.getage()>= lower && Entry.getage()<= upper){
     found.addEntry(Entry);  
     }

Link to comment
https://linustechtips.com/topic/277697-need-help-declaring-a-variable/
Share on other sites

Link to post
Share on other sites

What is 'Entry' exactly declared as?

2 things: the forum has a built in code function, so you more easily import some code to your post and you shoul always tell the language and purpose so we know what to do :)

And @airfrog19 follow your topics

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

What is 'Entry' exactly declared as?

2 things: the forum has a built in code function, so you more easily import some code to your post and you shoul always tell the language and purpose so we know what to do :)

And @airfrog19 follow your topics

i just copied and pasted it. 

Link to post
Share on other sites

Hi,

 

You have declared "Entry" inside your for loop but you are trying to access it from outside it. Either declare it outside or globally for the whole class.

 

Another thing, I would suggest not using capital letters for the start of variable names, it's bad form and can get confusing as usually classes are declared with a starting cap.

Link to post
Share on other sites

Hi,

 

You have declared "Entry" inside your for loop but you are trying to access it from outside it. Either declare it outside or globally for the whole class.

 

Another thing, I would suggest not using capital letters for the start of variable names, it's bad form and can get confusing as usually classes are declared with a starting cap.

should i put it right before the for loop?

Link to post
Share on other sites

That would fix that particular issue, yes.

There are a few other issues, but since it seems you're learning I'll let you figure those out since it's more fun. Eg: You could also merge the "Addressbook" declaration to one line, and then do something with it, such as returning it at the end of the method, otherwise it seems to be doing nothing :)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×