Equals() method is not working / always giving FALSE

package CodeChef;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CodeChefRAINBOWA{

public static void main(String[] args) throws java.lang.Exception {
 BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
 try {
	int t = Integer.parseInt(sc.readLine());
	while(t-->0) {
		int num = Integer.parseInt(sc.readLine());
		String str = sc.readLine();
		int start = str.indexOf('7');
		int last = str.lastIndexOf('7');
		String subString1= str.substring(0,start-1);
		String subString2 = str.substring(last+1);
		 StringBuilder input1 = new StringBuilder(); 
		 input1.append(subString2); 
		 input1= input1.reverse();
		  String compare = input1.toString();
		// System.out.println(subString1.toString().equals(compare));  ignore this  
		 
		// ISSSUE IS HERE! always giving false
		 if(subString1.equals(compare)) {
			 System.out.println("yes");
		 }
		 else
			 
			System.out.println("no"); 
			 
	}
	 
} catch (Exception e) {
	
} 
 


}

}

Please either format your code or link to your submission :slight_smile:

Can you give a testcase that shows the issue you’re having?

Edit:

Your code will throw an Exception (which is silently smothered) if the input does not contain a 7.

Edit2:

Given the testcase

1
13
1 2 3 4 5 6 7 6 5 4 3 2 1

(which should output yes), subString1 and compare are not the same string - can you see why?

1 Like

I havent submitted the code yet …this is the problem code RAINBOWA

package CodeChef;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CodeChefRAINBOWA{

	public static void main(String[] args) throws java.lang.Exception {
     BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
     try {
		int t = Integer.parseInt(sc.readLine());
		while(t-->0) {
			int num = Integer.parseInt(sc.readLine());
			String str = sc.readLine();
			int start = str.indexOf('7');
			int last = str.lastIndexOf('7');
			String subString1= str.substring(0,start-1);
			String subString2 = str.substring(last+1);
			 StringBuilder input1 = new StringBuilder(); 
			 input1.append(subString2); 
			 input1= input1.reverse();
			  String compare = input1.toString();
			// System.out.println(subString1.toString().equals(compare));  ignore this  
			 
			// ISSSUE IS HERE! always giving false
			 if(subString1.equals(compare)) {
				 System.out.println("yes");
			 }
			 else
				 
				System.out.println("no"); 
				 
		}
		 
	} catch (Exception e) {
		
	} 
     


	}

}

1 Like

Thanks - there are some hints as to what is going wrong in my first post, above :slight_smile:

I am not getting it plz explain …:sweat_smile::sweat_smile: …sorry fr the trouble
thank you

Use the testcase I provided. Print out the values of subString1 and compare using e.g.

System.out.println("substring: >" + subString1 + "< compare: >" + compare + "<");

Observe that they are not the same, so that subString1.equals(compare) is false, and no is (incorrectly) output.

1 Like

i have tried debugging the code in eclipse …the problem is equals in always giving FALSE …inspite the fact that both the strings are having the same values.

ok ! :slightly_smiling_face:

13
1 2 3 4 5 6 7 6 5 4 3 2 1
substring: >1 2 3 4 5 6< compare: >1 2 3 4 5 6 <
no

This is the eclipse output

Yep - so - are subString1 and compare the same string, and so what should subString1.equals(compare) return - true or false?

its always giving FALSE i have tried printing the return boolean

It’s giving false because the strings you’re comparing aren’t equal (look at the output you posted above very carefully) :slight_smile: Your task is to debug why.

there is one extra SPACE , is that what u r trying to refer :thinking::expressionless::neutral_face:

1 Like

Yes! So subString1 and compare can (probably?) never be equal, because compare will always have an extra space. Now you have to find out how to prevent this.

1 Like

you are awesome…!!!
i could have never figured it out myself thank you so much.
I will try to fix it .:blush::blush

1 Like

No, you can’t ask about questions from ongoing contests!!

Please delete your post.

2 Likes

should i delete it ?

Definitely, yes!