0

Do i need getters and setters to accses a private class? Seems messy.

im making a bank login programme to accses a bank account using bankcard info and i have set password to private im using scanner and asked that bank.password = sc.nextlin(); if(bank.password.equals("password"){System.out.println("Logging in"); }else{System.out.println("Loggin Failed");} but even when password is correct it still says login failed.... thanks

24th Jul 2017, 12:56 PM
D_Stark
D_Stark - avatar
11 Answers
+ 2
Ahah. I think I found the problen. I remember I had this issue one time, it's a pain at first. So the problem is not conparing the string, it's how the input was done. Notice before you take the password you have: scn.nextInt(); This grabs the next integer in the inputStream. Then, you grab the nextLine. password = scn.nextLine(); However!! This is NOT the line 'after' nextInt(), because nextInt() never shifted the stream to the next line. It is actually grabbing the rest of the line on the nextInt() line. (Guess that sounds a bit confusing). So, you gotta shift the stream one line over before taking the entire line. (It will still work, but you'd have to input the password next to the number, instead of on a new line). Working code: (This is just the fixed stuff) System.out.println("Your Sort Code: " + bnk.sort_code); scn.nextLine(); // shift to next line bnk.password = scn.nextLine(); System.out.println("Your Password " + bnk.password); // used equals() if(bnk.password.equals("Password")) This worked for me on SoloLearn. More info here: https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo
24th Jul 2017, 3:57 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
Haha, yea. You can actually see how the the syntax works, but it may be even more confusing. I believe this is the actual source code to the scanner class: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/util/Scanner.java This is the documents of the scanner class, here you can find every method the scanner has and what they do: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
24th Jul 2017, 9:34 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
I'll assume there's no syntax errors in the code itself. However, if the password is private, how are you accessing it like: bank.password = .... If bank is another class, that would imply it is public (or default). Fix: bank.setPassword(sc.nextLine); if (bank.getPassword().Equals("password")) {} else {} Note that the way you wrote the if statement means the password must be 'password'. Perhaps just use sc.next() instead of sc.nextLine(); This is because next() will just take the word. And I assume spaces aren't allowed to separate a few words. Getter and setters are mainly for Encapuslation. It's up to you if you want to make them private, you will need to for things like group projects. https://stackoverflow.com/questions/10407877/what-is-the-point-of-getters-and-setters
24th Jul 2017, 2:25 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
Perhaps try equalsIgnoreCase? It will ignore capital letters. If you don't want that, just use equals(), but keep in mind caps matter. (equals() still worked for me) This code worked for me: (I just modified your main). public static void main(String[] args) { Bank_Card bnk = new Bank_Card(); Scanner scn = new Scanner(System.in); bnk.password = scn.nextLine(); System.out.println("Your Password: " + bnk.password); if(bnk.password. equalsIgnoreCase("Password")){ System.out.println("Success!"); // put what you had before in here } else { System.out.println("login Failed"); } }
24th Jul 2017, 3:26 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
I suppose a screenshot? Does CodePen use the scanner differentially? Try using next() instead of nextLine().
24th Jul 2017, 3:36 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
thats great faith worked perfect it is abit confusing but ill get use to it Dont you hate when you think you understand somthing and all of a sudden it throws you off by not working 😳 glad somone knows what im doing 👍 i wish there was a way to see how a syntax works behind the code i think that would be so helpful for me. your like a living syntax haha
24th Jul 2017, 5:36 PM
D_Stark
D_Stark - avatar
0
/*hey faith it looks like this i cut a chunk out as couldent fit it all in*/ import java.util.*; public class Bank_Card { public String name,surname; public long card_no; public int sort_code; private String password; public static void main(String[] args) { Bank_Card bnk = new Bank_Card(); Scanner scn = new Scanner(System.in); bnk.password = scn.nextLine(); System.out.println("Your Password " + bnk.password); if(bnk.password.equals("Password")){System.out.println("logging in...\n" + "Hello " + bnk.name + " " + bnk.surname);} else { System.out.println("login Failed"); }
24th Jul 2017, 3:19 PM
D_Stark
D_Stark - avatar
0
thats strange the code i showed you works in codepg on its own but it dosent work with the rest of the code. its shows up password and then a blank how can i show you the whole code using mobile its hard to explain.
24th Jul 2017, 3:33 PM
D_Stark
D_Stark - avatar
0
i have unlocked it so u can see it in my codes as i cant seem to insert it with mobile edit.. maby i can but i have to inlock it first
24th Jul 2017, 3:41 PM
D_Stark
D_Stark - avatar
0
sweet thanks!
24th Jul 2017, 9:46 PM
D_Stark
D_Stark - avatar