1

I want to check if the input is only 1 letter or more than 1, below is my code

public ResponseEntity<String> moReceiver(@RequestParam("data")String data)
String[]subKeyword=data.split(" ");
String subKwd=subKeyword[1];
    if(subKwd =="")
    {
     System.out.println("One letter"); 
    }
    else
    {
     System.out.println("More than One letter"); 
    }

tried to use

==""

but doesnt work.. :( ,and got this error --->Index 1 out of bounds for length 1

  • 1
    You need to count from `0`. – Marcin Orlowski May 06 '19 at 12:14
  • Possible duplicate of [Array "out of bounds"](https://stackoverflow.com/questions/25983096/array-out-of-bounds) – xxxvodnikxxx May 06 '19 at 12:17
  • @NajibRazak not, why do you think? You have the pretty well explained how it works with indexes in the linked post, kindly please just read it, thanks :). `I assume you got as input array just one item, so length is 1 , but the last - and only valid- index is 0` (common indexing across programming languages, just few are having indexes from 1), so then if you will access into `arr[1]` you will ofc get array out of bounds exception.. btw never use `==` for string comparison, `.equals()` is better option – xxxvodnikxxx May 06 '19 at 13:16
  • Like one actual letter or the word "One Letter"? – Adan Vivero May 06 '19 at 21:30

2 Answers2

4

Array indexes are zero-based and string comparison should be done using 'equals'.

String subKwd = subKeyword[0];
if(subKwd.equals("")){
  System.out.println("Zero letters");
}

Or you could also check the size of the string:

if(subKwd.length() == 0){
  System.out.println("Zero letters");
} else if(subKwd.length() == 1){
  System.out.println("One letter");
} else {
  System.out.println("More letters");
}

If your rely on the fact that your keyword is indeed the second 'field' in your data you'll need to add an additional check to see if your data indeed contains at least 2 fields:

String[] subKeyword = data.split(" ");
if(subKeyword.length > 1){
  String subKwd = subKeyword[1];
  if(subKwd.length() == 0){
    System.out.println("Zero letters");
  } else if(subKwd.length() == 1){
    System.out.println("One letter");
  } else {
    System.out.println("More letters");
  }
} else {
  System.out.println("No subkeyword present in data");
}
TheWhiteRabbit
  • 1,253
  • 1
  • 5
  • 18
  • doesnt work,because when (String subKwd=subKeyword[1] ) ->no sub keyword,it returns Index 1 out of bounds for length 1 – Najib Razak May 06 '19 at 12:32
  • Ok, if you rely on the second 'field' in your data you'll first need to check if your array contains at least 2 fields. In that case add a if(subKeyword.length > 1) test around the rest... I'll update my answer. – TheWhiteRabbit May 06 '19 at 12:39
1

Usually you don't want to use == with Strings. You should use the equals method. Also, there is a function called length() that you can use on Strings.

public ResponseEntity<String> moReceiver(@RequestParam("data")String data)
if (data != null){
    if(data.length() == 1)
    {
        System.out.println("One letter"); 
    }
    else
    {
        System.out.println("More than One letter"); 
    }
}

This should work for what you are trying to achieve

cobrius
  • 161
  • 12