If Statement Providing Wrong Output - java

I coded a banking program. I'm at the point where the user must enter their pin, and it must be compared to their pin which is taken from a text file. The problem is, I have an if statement, and the condition is met, but it keeps giving me an the statement as if it was not met.
I've tried switch statements and even using a boolean value, but it does not work
public String getPin(){
return pin;
}
String pin2 = lbl_Pin.getText();
System.out.println(pin2);
System.out.println(getPin());
// Allows the user to select what they want to do after they enter the
// correct pin
if (pin2.equals(String.valueOf(getPin()))) {
btn_SelectLoan.setEnabled(true);
btn_SelectWithdraw.setEnabled(true);
btn_SelectDeposit.setEnabled(true);
btn_Balance.setEnabled(true);
lbl_Loan.setVisible(true);
lbl_Withdraw.setVisible(true);
lbl_Deposit.setVisible(true);
lbl_Balance.setVisible(true);
}
// Displays invalid if the pin is not correct
else {
lbl_Pin.setText("Invalid");
}
I enter the correct pin, I am sure of this as I displayed the correct pin as well as the entered pin, and they are the same, but an invalid answer is provided

It might be an encoding issue. When you are reading the pin from the text file, please make sure that the encoding of the text matches with that of lblP=_Pin.
When you are reading the text file, you can mention the desired encoding while opening the file.

I found the issue. When getting the Pin from the text file, '/r' was added on as part of the value. So I just made a substring with the actual value of the pin.

Related

How to clear the input of file chooser component on command line in install4j?

The file chooser component always preserve the previous results on command line, just like
example file:
[/bin/example/file.txt]
If I enter a "space" or "Enter" key, the previous value "/bin/exampel/file.txt" will be chose. If I input another path, the value of "example file" will be changed, but how to empty the value of "example file" without changing my code? It's an easy thing in Windows, but I couldn't find anything that works in Linux.
If it's necessary to change my code, all I can think of is when the user input a specific string such as "[]", I call
context.setVariable("file chooser variable", null);
to clear the input manually, which doesn't seem very reasonable.
Is there any better method to clear the input?
In console mode, the empty string or a blank string signifies the acceptance of the default value, there is no way to interpret it as missing input.
You could use a "Console handler" form component to ask the questions yourself with something like this:
console.println("Current directory: " + context.getVariable("fileVariableName"));
if (console.askYesNo("Enter a different directory?")) {
String newDirectory = console.askString("New directory", "");
if (newDirectory.trim().isEmpty()) {
context.setVariable("fileVariableName", null);
} else {
context.setVariable("fileVariableName", new File(newDirectory));
}
}
return true;
Then set the "Visibility script" property of the file chooser component to !context.isConsole() so it is not displayed in console mode.

How to check if a certain user has reader-access to a document

For an xpage-application with java beans i need to check if a certain user(not current user) has reader-access to a document. All acceslevels above (Database ACL, XPage ACL...) can be taken for granted. Current User is always at least author.
Each document has one readerfield "readers" and three authorfields "creator","authors","AdminAuthor", last can be ignored,since it always only contains "[Admin]" for every document
Current idea is to get the groups of the user like showed here(Determine all groups for a defined user), loop through them and compare to the reader and author fields field content
Why i don't like it:
use of an undocumented API
horrible performance
Is there any better way to do so? Especially with nested groups in mind, so $ServerAccess view is not really an option.
Current code:
public boolean isReader(String notesName, String documentID){
try {
Vector<String> readers= getAllReaderFieldsValues(documentID);
if(readers.contains(notesName)){
return true;
}
lotus.notes.addins.DominoServer server = new lotus.notes.addins.DominoServer(DominoUtils.getCurrentSession().getServerName());
for(String group:(Vector<String>)server.getNamesList(notesName)){
if (readers.contains(group)){
return true;
}
}
} catch (NotesException e) {
//ErrorHandling
}
return false;
}
Thanks for any help
There are different ways to check if a user has access to a document, but all of these are undocumented (but still useable since a decade), so they won't fit your requirements (i.e. running in a different user context or a special view with a "$C1$" column, ...)
A "documented" way to do what you want is just to add a user to a reader field, if his name is not already in the list. There is no need to check if the user has access or not.
I still wondering about your scenario, because I don't understand what you are trying to realize: You are checking if a user is in a specific group which gives him access to a document. If the user is in one of these groups, you skip his name.
In the meantime, the user is removed from the group, and has no longer access to the document...
Why not working with groups or roles? No coding, just administration. Are you fixing organizational problems?

Also show error message when value is empty during form validation

The problem I have is actually even visible in the official SmartGWT demo here: https://www.smartclient.com/smartgwt/showcase/#form_validation_regexp
If you enter nothing (leave field empty) and press validate, no error is displayed. For required value, I need the error shown even when the field is empty.
I set my validator to this:
RegExpValidator regExpValidator = new RegExpValidator();
regExpValidator.setExpression("^[0-9A-Z_]{7,12}$");
regExpValidator.setErrorMessage("Code must contain capital letters and numbers");
codeField.setValidators(regExpValidator);
Now this expression does NOT match an empty string. Yet, I get no error on validation.
How to show errors for empty required values in forms?
You can directly use setError method. And return the form back.
if(codeField.getText().trim().isEmpty()){
codeField.setError("The Code must not be Empty.");
return;
}
This will give the form back and also validate the empty string.

How to get rid of certain parts of a string input?

I just started my first programming class and I am trying to write a program that uses JOption dialogue boxes and can't seem to figure out how to create the right output. I am trying to have the user enter a URL into the dialogue input box, and have an output dialogue box display just the website name without the www. or the .com portion. Thanks in advance!
public static void main(String[] args)
{
String input= JOptionPane.showInputDialog(null,
"Please enter website URL");
String.output=JOptionPane.showMessageDialog(null,
)
There is a method in java called replace() that would be perfect for this scenario.
For learning knowledge, the method looks like this(it is built in) :
public String replace(char oldChar, char newChar)
But in order to call it, you can put in your string . replace of course with the parameters.
input.replace("www" , "") //Makes the www blank
I recommend putting these into switch/if statements.
if (input.contains ("www") {
input.replace("www" , "") //Makes the www blank
}
Now, if you also want to get rid of .com if it exists, then:
if(input.contains(".com"){
input.replace(".com", "")
}
Thats it. First, check if the user entered the stuff you don't want using .contains, and if they did remove it using .replace.
~{Rich}

Hide/Show Password in a JTextFIeld (Java Swing)

So I've been working on a Password Strength Checker and the way it works is that the user enters some random text into a textfield and then instantaneous visual feedback (breakdown of points) is displayed. I've also added a checkbox, which on being selected, should hide the password i.e. replace all the chars with asterisks, while preserving the actual text input by the user. A document listener is being used to keep track of changes inside the textfield. (each char on entry gets analyzed and then scored)
So, my question is, how exactly do I mask the user input with asterisks preserving its original value?
Here's what the GUI looks like:
http://speedcap.net/sharing/screen.php?id=files/51/2f/512f9abb3f92a25add7c593e9d80e9e4.png
How exactly do I mask the user input with asterisks preserving its original value?
Use the JPasswordField which has nice function jPasswordField.getPassword(); to get the password as char[] to work with.
Use jPasswordField1.setEchoChar('*') to mask the password characters with *.
If you wish to see the value you are inserting use jPasswordField1.setEchoChar((char)0); Setting a value of 0 indicates that you wish to see the text as it is typed, similar to the behavior of a standard JTextField.
Tutorial and Reference:
How to use Password Fields
setEchoChar(char)
Use Password Field Instead of using textfield
ok thanks for tutorialnya,
and ex,
action chechbox / double click
private void lihatActionPerformed(java.awt.event.ActionEvent evt) {
if (lihat.isSelected()) {
password.setEchoChar((char)0); //password = JPasswordField
} else {
password.setEchoChar('*');
}
}
I solved it as follows:
if ( jPasswordField.getEchoChar() != '\u0000' ) {
jPasswordField.setEchoChar('\u0000');
} else {
jPasswordField.setEchoChar((Character) UIManager.get("PasswordField.echoChar"));
}

Categories