How to use trim

if i ask the user to input 3 integers in one gui box and the integers can be spaced out as much as possible…how would i be able to find each integer and give the sum of the 3 integers using the trim and indexof methods?

I think the class “java.util.StringTokenizer” can help to solve your problem.

Regards,

Antony.

The below code may help you in solving your problem

/*
	This logic is not only for Three integers. It can work for more than three integers
	USAGE : convert("      0 0 0 0 6 7 9 1  0 0   0   0 0 0  1 2 3 9 5    ");
*/
public static void convert(String str)
{
	int addition  = 0;
	boolean found = false;

	int index = -1;
	int prevIndex = -1;

	for(int i=0; i <= 9; i++)
	{
		index = str.indexOf(i + "");

		if(index != -1 && found)
			index = str.indexOf(i + "",prevIndex);

		if(index != -1)
		{
			addition = addition + i;
			found = true;
			prevIndex = index  + 1;
			i--;
		}
		else
			found=false;
	}

	System.out.println("Addition ------> " + addition);

}

Sreekanth,

Your code works, however I can see a problem which will arise - two digit numbers. E.g

String s = “32 m helloworld 3 6”;
convert(s);

Should print out 41, however, the code above only prints 14.

I think the best solution is to use StringTokenizer as Antony has suggested.

Martyn,

There are many solutions if we can use the complete Java String API. But the question is to use only “indexOf”. Itz a little bit complicated using only “indexOf, trim()”. Anyway, you have raised a nice question

regards
sreekanth

martyn,

package java.util.regex provides efficient way to perform this operation
without using trim() and indexOf(),

import java.util.regex.*;

class addition
{
public static void main(String args[])
{
String str=“32 m helloworld 3 6 “;
Pattern pat=Pattern.compile(”[a-z ]+”);
String array[]=pat.split(str);
int sum=0;
for(int i=0;i<array.length;i++)
{
sum+=Integer.parseInt(array[i]);
}
System.out.println(“sum:”+sum);
}
}

This program prints the correct value:41

Hi Noori,

If you were just processing Strings, something like this would remove ALL the whitespaces in a String:

class RemoveWhiteSpacesTest {
public static void main (String args[]) {
String s = “Hello World. This is a TE ST o n l y”;

String a = s.replaceAll(" ", “”);
System.out.println("BEFORE: " + s);
System.out.println("AFTER: " + a);
}
}

Hope that helps.

Martyn

Do there have any method like trim to remove the white space?

String st = " Fox "
st = st.trim()

very easy!!!

Almir,

trim() only removes leading and trailing spaces. My example removes the spaces in between words as well.

Martyn

Kalin Ivanov

but what if i have to remove white spaces in between in Java.

Reshmi,

I posted it above, but here it is again.

class RemoveWhiteSpacesTest {
public static void main (String args[]) {
String s = “Hello World. This is a TE ST o n l y”;

String a = s.replaceAll(" ", “”);
System.out.println("BEFORE: " + s);
System.out.println("AFTER: " + a);
}
}

Hope this helps.

Martyn

how do you trim out spaces from a string and and (.+=>< and so on) cheers colin

Hi Coco,

A quick and nasty way is to convert the String into a character array with the .toCharArray() method. Once you do that, you can check each character with isLetterOrDigit(char ch) [returns a boolean]. If it is true, then append to a new StringBuffer. If false, just ignore that character.

Ugly, but it should work.

Hope this helps.

Martyn