Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everybody.
I have simple problem I can't resolve.
Java
String body="example....string...here.."; 
for(int loop=0;loop<body.length()-1;loop++)
{
    String cek1=body.substring(loop, loop+1);
    String cek2=body.substring(loop+1, loop+2);
    if(cek1=="." && cek2==".")
    {
       body=body.substring(0, loop)+body.substring(loop+2, body.length());
       loop=loop-1;			  
    }
}


Very simple but why i can't go to :
Java
if(cek1=="." && cek2==".")
{
   // HERE
    // bla bla bla bla
}

Please help me, I new in android.
Posted
Comments
Shubhashish_Mandal 23-Dec-13 6:56am    
not so clear..
bagus bujangga 23-Dec-13 7:06am    
I Never meet cek1="." and cek2="."
Shubhashish_Mandal 24-Dec-13 1:47am    
Issue is with the comparison. You have checked whether two objects reference (using == operator) is same instead the value of the object. So to check the value you should use
".".equals(cek1) && ".".equals(cek2)

Hi friend,

I Solved it :) Please mark it as solved and rate it too if you wish :)

Here's the code :
Java
String body = "example....string...here..";

for (int loop = 0; loop < body.length() - 1; loop++)
{
    char cek1 = body.substring(loop, loop+1).charAt(0);
    char cek2 = body.substring(loop+1, loop+2).charAt(0);

    if (cek1 == '.' && cek2 == '.')
    {
        body = body.substring(0, loop) + body.substring(loop+2, body.length());
        loop = loop-1;
    }
}


What I did was converted the single charater String type to a character type and then compare it :)


With Regards
Tushar Srivastava
 
Share this answer
 
If I understand your code correctly, you are trying to remove the '.' in a word. But your code is hard to comprehend. You should use regex pattern and matcher to achieve it. See my sample code below. You can find many regex tutorials on the web. I have one article on this here Code Experiment on Integer Validation[^]

package com.peterleow;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test01 {
	
	public static void main(String[] args) {
		   
	    String body = "example....string...here..";
	    
	    System.out.println("Before : " + body);
	    
            // regex syntax for one or more '.' 
	    Pattern pattern2Remove = Pattern.compile("\\.+"); 
	    
	    Matcher matcher = pattern2Remove.matcher(body);
	    
	    while(matcher.find())
            {
                 String string2Remove = matcher.group();

                 body = body.replace(string2Remove, "");

                 System.out.println("After : " + body);
            } 
	}
}
 
Share this answer
 
v2
Comments
Maciej Los 23-Dec-13 11:26am    
+5
Peter Leow 23-Dec-13 11:28am    
Thank you.
If you want to compare string, you cannot use
if (cek1 == '.' && cek2 == '.')

you have to use
if (cek1.equals"." && cek2.quals".")

because string is an object. of course, it's fine to use "==" to compare char :)
 
Share this answer
 
Comments
Shubhashish_Mandal 24-Dec-13 1:48am    
+5. didn't see the post before comment

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900