When compare two values in Jtextfield it is coming error when space also including it is showing correct. But without using it is showing error. for example in JTextfield1 it is "abcd" when in JTextfield2 "abcd " to show the message correct else it is showing error.
But I want to show if JTextfield1 is showing is "abcd" and same in jTextfield2 also same as "abcd" then show the message correct else it is false in jLabel.
my code is as follows
private void jTextField2KeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
if (jTextField1.getText().equals(jTextField2.getText()))
{
jLabel1.setText("sucess");
}
else
{
jLabel1.setText("fail");
}
}
when space also including it is showing correct.
Use this to avoid spaces
if (jTextField1.getText().trim().equals(jTextField2.getText().trim()))
From the doc public String trim()
Returns a copy of the string, with leading and trailing whitespace
omitted. If this String object represents an empty character sequence,
or the first and last characters of character sequence represented by
this String object both have codes greater than '\u0020' (the space
character), then a reference to this String object is returned.
Otherwise, if there is no character with a code greater than '\u0020'
in the string, then a new String object representing an empty string
is created and returned.
Related
I have a .txt file which contains following text:
111000111001
x00000010001
111110000001
I want to put this content into string so I use this method.
public void read() {
FileHandle file = Gdx.files.internal("map.txt");
String text = file.readString();
System.out.println(text.charAt(12));//Here is the problem,it's showing empty character instead of x
}
When I want to get the 12th element(x on 2nd line),it's impossible(I think there's a problem of passing to new line,but I don't know how to solve it).Can you help me please?
There's something called Carriage Return that makes extra characters appear at the end of each line (3 extra characters to be precise) when reading from a text file so to avoid getting those in your way you can use:
text = text.replaceAll("(?:\\n|\\r)", "");
And now when you try to print the 12th element you get the "x" you wanted
System.out.println(text.charAt(12)); // Prints x
Here's more info about the replaceAll() method:
Java Api: String.replaceAll()
I have a program that is reading a text file that has a list of items, creates an ArrayList consisting of the items it reads, then compares it to a few chosen words. For example, my text file contains this (without the numbering):
book
desk
food
phone
suit
and it reads each one and adds it to an ArrayList. When I tried comparing a String s = "book" to each element in the ArrayList, I find that s is not equal to anything. This is what I have in a method:
for (int i = 0; i < list.size(); i++)
{
if (s.equals(list.get(i))
return true;
}
s.contains() doesn't work either. When I print the ArrayList, there's an additional whitespace at the end of each String element. How can I get the comparison to work when s = "book"? Why is there additional whitespace?
Use trim() to remove leading and trailing whitespace.
if (s.equals(list.get(i).trim())
return true;
}
instead of
if (s.equals(list.get(i))
return true;
}
You can use the trim() method of the String class to remove whitespaces in the start and the end of the string.
The whitespaces may be in the file but you didn't notice it. Check with an editor that there are no spaces at the end. To be more sure, check with a hexadecimal editor like hd on unix.
Also, check that the read strings do not contain the line feed character.
Well, I'm trying to replace a word by using contains() Method:
String z = tfB.getText().toString();
String show = textPane.getText().toString();
if(show.contains(z)){
// how I specify the word that were found and change it without
effecting anything with in that line
}
well what I main by that:
What I'm trying to do is get the value from the user.
then search if it found replace it with something. For example:
String x = "one two three four five";
It should set the textPane to "one two 3 four five"
or
"one two 3-three-3 four five"
could any one please tell me how to do it.
Thank you
What I'm trying to do is get the value from the user. then search if it found replace it with something.
Don't use the contains() method because you will need to search the text twice:
once to see if the text is found in the string
again to replace the text with a new string.
Instead, use the String.indexof(...) method. It will return the index of the text IF it is found in the String.
Then you should replace the text directly in the Document of the text pane, not in the String itself. So the code would be something like:
int length = textPane.getDocument().getLength();
String text = textPane.getDocument().getText(0, length);
String search = "abc...";
int offset = text.indexOf(search);
if (offset != -1)
{
textPane.setSelectionStart(offset);
textPane.setSelectionEnd(offset + search.length();
textPane.replaceSelection("123...");
}
Also, not that you get the text from the Document, not the text pane. This is to make sure the offsets are correct when you replace the text in the Document. Check out Text and New Lines for more information on why this is important.
I have a problem with a contains (...) method from List <...> class. I'm trying to check if a expression (that is loaded from user input) already exist in a List, but if I entered same name as twice, it said there's nothing same in the List. Please help, there's source code:
boolean checker;
checker = expressions.contains(line[1]);
if (checker == true) {
System.err.println("This expression has already been declared!");
return index;
}
PS: line[1] is a second index in array from main function that stores user-entered line split by whitespaces. (First index of line need to be always 'var', and second is any word that cannot be twice in the List)
Your list may not have exact same string as provided in the input which may be due to white spaces. Try trimming the input and then call contains
checker = expressions.contains(line[1].trim());
What will be the FormatterFactory's factor value in JFormattedTextField if I only want to accept letters and spaces.
Cause I want it to accepts Names only. Like - John Doe.
I couldn't find an elegant way using a formatter. The non-elegant way is to create a MaskFormatter with the main issue that you will be limiting the number of characters allowed (although you can limit to an arbitrarily large number).
MaskFormatter mask = new MaskFormatter("*************"); // Specifies the number of characters allowed.
mask.setValidCharacters("qwertyuiopasdfghjklzxcvbnm" +
" QWERTYUIOPASDFGHJKLZXCVBNM "); // Specifies the valid characters: a-z, A-Z and space.
mask.setPlaceholderCharacter(' '); // If the input is less characters than the mask, the space character will be used to fill the rest. Then you can use the trim method in String to get rid of them.
JFormattedTextField textField = new JFormattedTextField(mask);
I feel that validating the input is a better approach than restricting characters in this case. I can add an example if you want to use this approach.
Edit: Using InputVerifier, you have to subclass it and override verify as shown below.
JTextField textField = new JTextField();
textField.setInputVerifier(new InputVerifier() {
#Override
public boolean verify(JComponent input) {
String text = ((JTextField) input).getText();
if (text.matches("[a-zA-Z ]+")) // Reads: "Any of a-z or A-Z or space one or more times (together, not each)" ---> blank field or field containing anything other than those will return false.
return true;
return false;
}
});
The text field will not yield focus (except to parent components) until requirements are met.