Вопрос

Please have a look at the below code

while (reader.hasNext()) 
          {
          JsonParser  _parser = new JsonParser();
          JsonElement jsonElement =  _parser.parse(reader);
          JsonObject jsonObject1 = jsonElement.getAsJsonObject();

              int totalNumberOfUniqueWords=0;

          //Add Title and Body Together to the list
          String titleAndBodyContainer = " My Name is something that you don't know. you know that anyway? My Name?";

              //Remove full stops and commas
              titleAndBodyContainer.trim();
          titleAndBodyContainer = titleAndBodyContainer.replaceAll("[^a-zA-Z'\\s]+", " ");
          titleAndBodyContainer = titleAndBodyContainer.toLowerCase();
              titleAndBodyContainer.trim();

              StringBuffer wordList = new StringBuffer("");

              //Removing Duplicate Words
              HashSet<String>noDup = new HashSet<String>();
              String[]titleAndBodyContainerArray = titleAndBodyContainer.split(" ");

              for(int i=0;i<titleAndBodyContainerArray.length;i++)
              {
                  if(!noDup.contains(titleAndBodyContainerArray[i].trim()))
                  {
                      wordList.append(titleAndBodyContainerArray[i].trim());
                      wordList.append(";");
                      noDup.add(titleAndBodyContainerArray[i].trim());
                      break;
                  }
              }

              System.out.println(wordList.toString());
              break;


              //index++;
          }

In all the time, the output is ;. This simply means white space in front of the sentence is not removed! There is no output if I replace the titleAndBodyContainer to be "My Name is something that you don't know. you know that anyway? My Name?", because it has no white space in front of the sentence. What is wrong here?

Это было полезно?

Решение

titleAndBodyContainer.trim() does not trim the original string, but returns a trimmed version of it.

Try

String trimmedTitleAndBodyContainer = titleAndBodyContainer.trim();

then process the result.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top