How can I make a JTextfield
accept only Hebrew letters with if statement?
I can do a long if statement with all the Hebrew letters but it will not look good.
I found out that the Unicode of Hebrew first letter is \u05D0
and last one is \u05EA
.
How can I say that if the gettext is in between these 2 letters so show (meaning to check if the text entered is only a Hebrew letter), the user will add only one letter in each textfield.
Thank you in advance

- 3,887
- 1
- 23
- 34

- 69
- 7
-
2Do you mean `if (0x05D0 <= ch && ch <= 0x05EA)` to check it is ok. – Peter Lawrey May 07 '16 at 15:38
-
@PeterLawrey Yes something like that – Aboelmagd Saad May 07 '16 at 15:40
5 Answers
Build an input validator with your validation logic, and attach it to your textField to verify input as you enter it. Steps: Combine the validation logic given by @peter-lawray with the mechanism of building an input verifier and you're good to go.

- 1
- 1

- 2,277
- 1
- 23
- 34
-
@mohsenmadi Thank you so much, but I feel this is a little more advanced for me – Aboelmagd Saad May 07 '16 at 16:31
-
No problem :-). If I were you and you are new to all this, I would invest the time into learning JavaFX if you want to build desktop apps. JavaFX is the new GUI framework. – mohsenmadi May 07 '16 at 17:43
You could use a simple one-liner using a Stream
boolean valid = jTextField.getText().chars().allMatch(p -> p <= 0x05ea && p >= 0x05d0);

- 1,118
- 10
- 14
Putting the other answers together, this is an input validator you could use:
// adapted from mohsenmadi/Daniel Rikowski
public class HebrewVerifier extends InputVerifier {
@Override
public boolean verify(JComponent input) {
String text = ((JTextField) input).getText();
// method suggested by Mad Matts
return text.chars().allMatch(p -> p <= 0x05ea && p >= 0x05d0);
}
}
And then you simply need to attach it to your JTextField
:
myHebrewTextField.setInputVerifier(new HebrewVerifier());

- 13,664
- 17
- 79
- 131
Since you are using JTextField
and this class inherits getText()
method which returns a String
. So, this is how I will probably do it.
String name = jTextField.getText();
char[] charArray = name.toCharArray();
for (char c : charArray) {
if (!(c <= 0x05ea && c >= 0x05d0)) {
break;
//valid
}
}
This can become more efficient, if you keep the counter of elements added/removed, you only will have to check the latest entered character (in case of removal you probably won't need that but that scenario will need more coding, so I hope you will figure that out once you solves this issue).
Update:
This is what I have tried:
String name = "אבגa";
char[] charArray = name.toCharArray();
for (char c : charArray) {
if (c <= 0x05ea && c >= 0x05d0) {
System.out.println("Valid hebrew");
}
}
And this prints Valid hebrew
three times.

- 464
- 3
- 11
-
Thank you really. But some reasons it doesn't work for me ! Can we make the char String ? – Aboelmagd Saad May 07 '16 at 16:01
-
I tried it but it seems the check is not done ` if (btn.getSelectedItem().equals("\u05E2\u05D1\u05E8")) { String first; Swing get = new Swing(); first = get.getfl(); char [] charArray = first.toCharArray(); for(char c : charArray) { if (c <= 0x05ea && c >= 0x05d0) { JOptionPane.showMessageDialog(null, "error"); } }` – Aboelmagd Saad May 07 '16 at 16:11
-
@AboelmagdSaad Can you tell me the input you have in your textfield? – java8.being May 07 '16 at 16:12
-
sure, I tried with english letters like (a or b) and then went to the combobox and chose the first action I want , and no errors happened at all – Aboelmagd Saad May 07 '16 at 16:14
-
@AboelmagdSaad Check the updated answer. Code is checking for valid herbrew characters. If these are valid, it will go inside the `if` otherwise, not. – java8.being May 07 '16 at 16:16
-
I knew the error :D I missed to add != if (!(c <= 0x05ea && c >= 0x05d0)) Now it works – Aboelmagd Saad May 07 '16 at 16:19
-
done :) Thank you so much But can we run the check only one time, because for now, the error message appears twice for me and not only one time ! – Aboelmagd Saad May 07 '16 at 16:26
-
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
//...
public static String readHebrewString()
{
String str="";
System.out.println("הקלד שורה");
InputStreamReader in;
try {
char[]buffer=new char[1024];
in = new InputStreamReader(System.in, "Windows-1255");
in.read(buffer);
int i=0;
while((int)buffer[i]!=10)
str+=buffer[i++];
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Demo001.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Demo001.class.getName()).log(Level.SEVERE, null, ex);
}
return str;
}

- 1
-
1Every time you use `+=` on `str`, a new `string` object is created. The memory consumption of your loop is the amount of characters squared and then divided by 2. – Peter Bruins Oct 17 '22 at 12:48