5 Replies - 11866 Views - Last Post: 20 February 2011 - 07:08 PM

#1 SHENGTON   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 10-October 08

How to add a space in an array in J2ME?

Posted 18 February 2011 - 10:52 AM

Hello, good morning. :)

I'm thinking if the user forgot to put a space after the period or question mark or exclamation mark in the textbox like this:

Quote

i luv u.hu s ds pls?


The whole code is working and fine, but it doesn't give or add a whitespace everytime the period or exclamation mark or question mark is in the middle of the two characters in an array.

So what I want to happened is, if the user forgot the space:

Quote

i luv u.hu s ds pls?


After pressing the send command, the output should be this:

Quote

I love you. Who is this please?


This is still part of my SMS Textspeak Corrector.

Don't worry about the correcting textspeak because I already have the code for that. I just need to add a space after the period if the user forgot to put a space in his text message.

This is the code for capitalizing the first letter in every sentence:
  public static String capitalize(String temp)
  {
      char[] arr = temp.toCharArray();
      boolean cap = true;
      boolean spacefound = true;
      for (int i = 0; i < arr.length; i++)
      {
          if (cap)
          {
              if (arr[i] == ' ')
              {
                  spacefound = true;
              }
              else
              {
                  if (spacefound && !Character.isUpperCase(arr[i]))
                  {
                      arr[i] = Character.toUpperCase(arr[i]);
                  }
                  cap = false;
                  spacefound = false;
              }
          }
          else
          {
              if (arr[i] == '.' || arr[i] == '?' || arr[i] == '!')
              {
                  cap = true;
                  spacefound = true;
              }
          }
      }
      return new String(arr);
  }


Is This A Good Question/Topic? 0
  • +

Replies To: How to add a space in an array in J2ME?

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: How to add a space in an array in J2ME?

Posted 18 February 2011 - 11:59 AM

Hey.

You could do that using a regular expression.
public static String addSpaces(String input)
{
	return input.replaceAll("([\\.?!])([^ ])", "$1 $2");
}


Basically searches for any two chars sequences where a ".", "?" or "!" is not followed by a space, and adds a space.
Was This Post Helpful? 0
  • +
  • -

#3 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: How to add a space in an array in J2ME?

Posted 18 February 2011 - 12:25 PM

Just out of interest, I created a regular expression version of your capitalize method, including the space add thing.
public static String capitalize(String temp)
{
	// To help construct the output string
	StringBuilder sb = new StringBuilder();
	
	// Build a pattern to find all ".", "?" and "!" chars, the spaces that
	// follow them (if any) and the lower-case letter that follows that (again
	// if any).
	Pattern p = Pattern.compile("[\\.?!]( *)([a-z]?)");
	Matcher m = p.matcher(temp);
	
	// Buffer variables.
	int istart = 0; // The end index of each match
	String seperator; // Chars seperating the .?! and the first char.
	String firstChar; // First char in the string after the .?!
	
	// Loop through all the matches.
	while (m.find())
	{
		// Get the seperator and first char strings. Note that if there
		// aren't any spaces or if the first letter isn't lower case, their
		// respective strings will be empty.
		seperator = m.group(1);
		firstChar = m.group(2);
		
		// Add the preceding string, up to and including the symbol.
		sb.append(temp.substring(istart, m.start(0) + 1));
		
		// Add the spaces.
		if (seperator.length() == 0)
		{
			// There was no space, so we make one.
			sb.append(" ");
		}
		else
		{
			sb.append(temp.substring(m.start(1), m.end(1)));
		}
		
		// Check if a lower case letter was found and add it's upper case
		// equivelant if it was.
		if (firstChar.length() > 0)
		{
			sb.append(Character.toUpperCase(firstChar.charAt(0)));
		}
		
		// Move the istart index to the end of this match.
		istart = m.end();
	}
	
	// Add the rest of the string to the new string.
	sb.append(temp.substring(istart));
	
    return sb.toString();
}


Was This Post Helpful? 0
  • +
  • -

#4 SHENGTON   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 10-October 08

Re: How to add a space in an array in J2ME?

Posted 18 February 2011 - 07:00 PM

Hello, good morning. :)

@Atli
I'm using a J2ME language. I think the Pattern and Matcher is not available in J2ME API.
Was This Post Helpful? 0
  • +
  • -

#5 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: How to add a space in an array in J2ME?

Posted 18 February 2011 - 11:47 PM

OK, I see.
To be honest I'd stay well clear of that thing..... A library without regular expression support :no:

But anyways, I guess you'll just have to go through the string char by char searching for .?! chars and checking if there is a space after them.
You could use the String.charAt method to read each char in a loop, and the char after it, to find places where spaces need to be added. Then you can use String.substring and the StringBuilder class (assuming it is available) to cut the input string into pieces at those indexes and rebuild it with the added spaces.
Was This Post Helpful? 0
  • +
  • -

#6 SHENGTON   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 10-October 08

Re: How to add a space in an array in J2ME?

Posted 20 February 2011 - 07:08 PM

Hello Atli, :)

I created another function for adding a whitespace.

The code below will result like this:

Quote

I love u.Hu is this please?


The result should be:

Quote

I love you. Who is this please?


  public static String space(String temp)
  {
      char[] arr = temp.toCharArray();
      for (int i = 0; i < temp.length(); i++)
      {
          if (arr[i] == '.' || arr[i] == '?' || arr[i] == '!')
          {
              arr[i] = ' ';
          }
      }
      return new String(arr);
  }


Edited by macosxnerd101: With 112 posts, you should know the rules. Use code tags, NOT quote tags when posting code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1