This question might have been asked several times but I didn't find a proper answer so I am posting this question. What I want to do is validate the Japanese text entered in the edit text field to allow only half-width Japanese characters. I only want to check the validation once user enters the text and taps on some action button.
I've created method validates if typed character is half-width kana:
public boolean validate(String c) {
Pattern pattern = Pattern.compile("[\uff61-\uff9f]");
return pattern.matcher(c).matches();
}
I understood that you want to check is written text composed of only half-width kana, yes? To do this, in onClick() of button which you clicking for validate, write something like this:
for (int i = 0; i < textToValidate.length(); i++) {
if (validate(textToValidate.charAt(i))) {
continue;
} else {
System.out.println("Text wasn't written in half-width kana.");
return;
}
}
System.out.println("Text was written in half-width kana.");
Let me know if my answer is helpful for you. ;)
Okay so I came across many solutions and this one worked for me.
Basically, I have a Japanese half-width validation regex and compared it to my string.
val halfWidthRegex = "^[ァ-ン゙゚]+\\s?[ァ-ン゙゚]+$"
if(myText?.matches(halfWidthRegex.toRegex()))
// Valid halfwidth text
else // Invalid halfwidth text.
Related
I want to get the current letter in EdiText to String, I tried using some ways but without success, because I'm beginner and still learning.
what I want exactly is to insert special char after every written special char (in onTextChanged event), after get it into String to use (if ...equals ...)
String selected = edittext1.getText().toString().substring(startSelection);
Editable editAble = edittext1.getEditableText();
int start = edittext1.getSelectionStart();
if (selected.equals("(")) {
editAble.insert(start, ")");
}
this code not working.
any suggestions ?
I will do my best to explain my problem, sorry if I am not clear. Basically I want to set the color of individual characters of a text, and then display them in Android App Development Kit. The problem I am having is that I am taking an array of the characters, and don't know how to set the color of them to a certain hexadecimal value.
For instance if the String is "hello". I would want each character to be a different color.
So I would take 'h' and assign it the hexadecimal value of "#000000". And then display it using xml. Is this possible? Here is what I am attempting to do now with my code.
String end = "";
for (int p = 0; p < charzart.size(); p++) {
if (charzart.get(p).equals(" ")) {
}
else{
Spannable colorSpan = new SpannableString(charzart.get(p));
int fake = Integer.parseInt(color.get(p));
colorSpan.setSpan(new ForegroundColorSpan(fake), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
end += colorSpan;
}
}
output.setText(end);
Right now my code doesn't work and I am not sure why. So i am running through charzart which holds my characters. And then attempting to assign the hexadecimal value ( which I have in another array list called color). I check to see if there is a space, if there is I ignore it and move on until i reach a character. From there, I get the character, set it to a spannable. I then get the color, apply it to the spannable, add it to a string and at the end set the TextView to that string.
Basically I want to know how to assign a hexadecimal value to a character, which will then be outputted by XML.
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'm making a word guessing game. The JTextField can't be empty, can't be longer or smaller than 5 and cannot contain numbers. How do I do the last part?
#Override
public void actionPerformed(ActionEvent e) {
if (text.getText().isEmpty()) {
showErrorMessage("You have to type something");
} else if (text.getText().length() != 5) {
showErrorMessage("Currently only supporting 5-letter words");
} else if (contains integer){
showErrorMessage("Words don't contain numbers!");
} else {
r.setGuess(text.getText());
}
}
Rather than explicitly checking for numbers, I would suggest whitelisting characters which are allowed. Would letters with accents be allowed, for example? Both upper and lower case? Punctuation?
Once you've worked out your requirements, I suggest you express them as a regular expression - and then check whether the text matches the regular expression.
For example:
private static final Pattern VALID_WORD = Pattern.compile("^[A-Za-z]*$");
...
if (!VALID_WORD.matcher(text.getText()).matches()) {
// Show some appropriate error message
}
Note that I haven't included length checks in there, as you're already covering those - and it may well be worth doing so in order to give more specific error messages.
You might also want to consider preventing your text box from accepting the invalid characters to start with - just reject the key presses, rather than waiting for a submission event. (You could also change the case consistently at the same time, for example.) As noted in comments, JFormattedTextField is probably a good match - see the tutorial to get started.
create a method that checks if the JTextField has a number like this:
private boolean isContainInt(JTextField field) {
Pattern pt = Pattern.compile("\\d+");
Matcher mt = pt.matcher(field.getText());
return mt.find();
}
if (!text.getText().matches("[a-zA-Z]*")) {
// something wrong
}
Have been at this for a while so I think it's time for some much needed help. I have the following if statement in an action listener('but' - being the Search button that gets clicked for the following to happen).I have an arrayList(used to save data from database) in my class, calling a specific record works perfectly. I am trying to create a search textField(named 'tf' below). I have been able to search only by typing an exact match to what ever is being searched, but I want to be able to use partial matching on any String in tf. Everything that's commented out is different stuff that I have been testing, therefore can be ignored.
The statement:
if( srch.getText().equals("You have chosen to search by movie: " )
... this is used to make sure that the person is searching only when the panel includes this title, as there is a genre search as well on the same panel. Only the title changes.
if(source.equals(but)){
//String b = a.get(0).get(1);
String result = tf.getText();
Pattern pat = Pattern.compile("[a-z]+");
Matcher m = pat.matcher(result);
//boolean bool = m.matches();
int i = 0;
//al.contains(result) && pat.matcher(a.get(i).get()).matches())
// && z.contains(result)
if( srch.getText().equals("You have chosen to search by movie: " )){
for (ArrayList<String> al : a){
if (pat.m(a.get(i).get(0)).matches()){
System.out.println(a.get(i).get(0)); //movie name
System.out.println(a.get(i).get(1)); //movie desc
}
i++;
}
//ta.setText(b);
}
else{
ta.setText("Please try searching by movie");
}
}
In summary, everything works fine. I just need a regex code to find partial matches as well and a way to add that into my loops. I've added as little code as possible as not to waste anyone's time, so please let me know if any other is needed.Many thanks in advance. Eventually any full or partial matches will display the movie name and description.
I think there is no need of regex.
String.contains(String chars) returns true if the source string have any occurrence of the argument string.
For example:
String str = "ABCDEFG";
if(str.contains("CD")){
System.out.println("Present");
}