+ 2

My getters and setters for my private var - Java

Yello, I have this small little program where one class (passwordDB) acts as a sort of database which stores a private variable called 'password' which can only be accessed through getters and setters. Although it now partly does what I wanted it to do (allow the user to change the value of the private 'password' variable through the 'main class') I came across various issues which confused me. "this.password = x;" would no longer work unless I changed it to "password = x" (so what's the difference?) Also I had to set the private 'password' variable to static and even the getter method to static for all of this to work. Allot of what I have done is all experimental so I just would like an explanation of what is going on and whether I could have done it differently. Later on I plan to have a conditional statement for my setter method which will will specify something along the lines of: if(correctPass() == true){ password = x } boolean 'correctpass' will be set to true within the main class of the if statements once the user has specified the right password. And then the 'passwordDB' class will use the getter method of the main class to know if it should set the password. https://code.sololearn.com/cA30hxme03Yz/#java

10th Aug 2017, 4:06 PM
YodaCoda
YodaCoda - avatar
5 odpowiedzi
+ 4
'this' keyword, referrs to the current Object instance. Because your method is static, there is no instance to refer to. Remember static makes the member bound to the class, not an instance of the class. So, you cannot use the 'this' keyword inside a static member. If you remove static and create an Object of the class. Example/ PasswordDB pass = new PasswordDB(); pass.setPass(newPass); You will then be able to use the 'this' keyword.
10th Aug 2017, 4:09 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
That's because the scanner was expecting input but didn't get any. So when you wrote: String ask = input.nextLine(); If there was nothing to read, the exception is thrown. This is sort of a problem with how SoloLearn takes input, you have to properly input everything at once or you get that exception. You might be able to deal with it by checking if input.hasNext() before reading from the scanner. hasNext() will return true if there's more things to read in the input stream, so if that's empty then the user didn't enter enough input. Overall its more of a SoloLearn issue. This is likely why you don't see many Text-Based games that ask for user input on SoloLearn. Though, Sololearn has their reasons for doing this.
10th Aug 2017, 7:56 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
Thank youuuuuuu!!!! :D
10th Aug 2017, 5:00 PM
YodaCoda
YodaCoda - avatar
+ 1
Oh I understand. it's a shame that we can't do it. I did try it just now but it gave me a 'time limit exeeded' so I guess Sololearn does not allow calculations to be done of a certain limit. Hopefully in the future we can see a virtualized version of the compiler so that we can do whatever we want with it without breaching bandwidth and security issues.
10th Aug 2017, 9:04 PM
YodaCoda
YodaCoda - avatar
0
Hi, everything is finished and is working perfectly in IntelliJ, but for some reason when I paste it into solo learn's environment it throws an exception: Exception in thread "main" java.util.NoSuchElementException:. Any ideas why? https://code.sololearn.com/cUVBeNJJV9Sq/#java
10th Aug 2017, 7:37 PM
YodaCoda
YodaCoda - avatar