0

I have 6 words that is stored in a HashSet as below :

String first = jtfFirstWord.getText();
String second = jtfSecondWord.getText();
String third = jtfThirdWord.getText();
String fourth = jtfFourthWord.getText();
String fifth = jtfFifthWord.getText();
String sixth = jtfSixthWord.getText();
Set<String> accept = new HashSet<String>(Arrays.asList(new String[] {first,second,third,fourth,fifth,sixth}));

I want to check if the length of all words is not 7, 9, or 11, then I will prompt users with a message box to warn them to only enter 7, 9, or 11-characters word.

Currently, I'm using if-else statement to manually check the condition like:

if (first.length() != 7 || first.length() != 9 || first.length() != 11 || 
    second.length() != 7 ||second.length() != 9 || second.length() != 11 ||
... and it continues until the sixth word.

By using this way, it reduce my code readability and its not an elegant way of checking user inputs.

My question is how do I check the length of the String element stored previously in the HashSet shown above? Or is there any other elegant and efficient way of checking the inputs?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 1
    Have a look at input verifiers: https://stackoverflow.com/questions/2749521/how-to-validate-a-jtextfield – sn42 Mar 16 '18 at 08:38

2 Answers2

3

You can of course use a loop, checking that each element of the set has the one of the accepted lengths, and break out of the loop as soon as one string hasn't.

But you can also use Stream's allMatch method, which does that for you:

boolean allOK = accept.stream().allMatch(s ->
    s.length() == 7 || s.length() == 9 || s.length() == 11);

if (!allOK) {
    // display error message
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

If you are using Java 8 you can use Stream::allMatch

boolean check = accept.stream()
        .allMatch(input -> Arrays.asList(7, 9, 11).contains(input.length()));

Or :

List<Integer> acceptedLengths = Arrays.asList(7, 9, 11);
boolean check = accept.stream()
        .allMatch(input -> acceptedLengths.contains(input.length()));

I used Arrays.asList(7, 9, 11) to define the accepted length, you can add what you want here, Its more easier than using condition for each length.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • check will always be true. – JB Nizet Mar 16 '18 at 08:43
  • correct I don't analyse it correct it @JBNizet, I fix it, what do you think now? – Youcef LAIDANI Mar 16 '18 at 08:45
  • It still does the inverse of what the OP wants. He wants to check that all strings have a length of 7, 9 or 11. Your code checks the inverse. You would have to use anyMatch if you keep the predicate as is. – JB Nizet Mar 16 '18 at 08:47
  • @JBNizet I changed the logic now my solution not look like as your solution, what do you think? – Youcef LAIDANI Mar 16 '18 at 08:57
  • 1
    @YCF_L thanks for your reply. However, JB answer is simpler and more concise for me as I only need a simple input validation. –  Mar 16 '18 at 09:08