+ 1

whats wrong?

import java.util.ArrayList; public class Program { public static void main(String[] args) { Linoy l = new Linoy(); Tom t = new Tom(); if (t.tomProfile.contains (l.linoyProfile.get (0)) && t.tomProfile.contains (l.linoyProfile.get (1))){ System.out.println ("It's a Match!"); } } } public class Linoy{ public static void arr1(){ ArrayList <String> linoyProfile = new ArrayList <String> (); linoyProfile.add ("Brown hair"); linoyProfile.add ("Tall"); } } public class Tom{ public static void arr2(){ ArrayList <String> tomProfile = new ArrayList <String> (); tomProfile.add ("Brown hair"); tomProfile.add ("Short"); } }

9th May 2020, 9:43 PM
Yahel
Yahel - avatar
12 Answers
+ 3
You don't have access to what you are treating as members of your classes. The arr1 and arr1 methods are never called and even if they were the ArrayLists inside them would be immediately destroyed at the end of the methods. Instead you need to make the ArrayLists a member/property of the classes, then you can set and add values to them in their constructor. Also, note there will be no output do to one is Short and the other is Tall so they won't match. public class Linoy{ ArrayList <String> linoyProfile = new ArrayList <String>(); Linoy() { linoyProfile.add ("Brown hair"); linoyProfile.add ("Tall"); } } public class Tom { ArrayList <String> tomProfile = new ArrayList <String>(); Tom() { tomProfile.add ("Brown hair"); tomProfile.add ("Short"); } }
9th May 2020, 10:15 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Thanks guys very useful for me. 😊
10th May 2020, 10:45 PM
Nikhat Shah
Nikhat Shah - avatar
+ 2
Your main method is fine, but if condition returns false because 1 is Short and the other is Tall. So, there isn't any output. Either change them so they match, or change your if statement.
10th May 2020, 6:57 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
The constructor is just a special method that is called when instantiating an object. It is typically used to initiate values to members of the class amongst other things. If you needed to pass a value into the constructor they would be passed where you have (here). The this keyword is used to refer to the current instance of the object, within the class itself. You didn't necessarily have to use the constructor in this case. You could have just as easily made another method in the class, similar to the arr methods you had, and set the ArrayLists values there. You would, however, need to call these methods after instantiating the objects and before trying to access its values.
10th May 2020, 7:18 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
... public static void main(String[] args) { Linoy l = new Linoy(); l.init(); ... ... } ... public class Linoy{ ArrayList <String> linoyProfile = new ArrayList <String>(); public void init() { linoyProfile.add ("Brown hair"); linoyProfile.add ("Tall"); } }
10th May 2020, 8:01 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
void yes - fixed static no
10th May 2020, 8:07 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
ChaoticDawg thanks!
10th May 2020, 8:15 AM
Yahel
Yahel - avatar
0
ChaoticDawg explained this well enough, so I will just add refactored code. https://code.sololearn.com/c0zUGOrhLOyD/?ref=app
9th May 2020, 10:30 PM
Michal
Michal - avatar
0
ChaoticDawg thank you, but can you please sent the whole code because i need to see the code to understand how it goes into the main method...
10th May 2020, 6:53 AM
Yahel
Yahel - avatar
0
ChaoticDawg thanks, i did it! but can you explain why did we use a constractor? and when i could use it? and shouldnt it be with things that are writen here: Linoy l = new Linoy (here); and something to do whis "this" keyword?
10th May 2020, 7:07 AM
Yahel
Yahel - avatar
0
ChaoticDawg how would it look/be with the other option? Not a constructor...
10th May 2020, 7:52 AM
Yahel
Yahel - avatar
0
ChaoticDawg don't you need a static and a void keyword after public? And then the name of method?
10th May 2020, 8:06 AM
Yahel
Yahel - avatar