0

When i put for(j=1;j<str1.length();j++) so i am getting different answers why??

public class Program { public static void main(String[] args) { String str1="ab"; byte number=0; //char name[]=str1.toCharArray(); for(int i=0;i<str1.length();i++) { for(int j=i+1;j<str1.length();j++) { if(str1.charAt(i)==str1.charAt(j)) { System.out.print(str1.charAt(i)+" "); System.out.print(str1.charAt(j)+" "); number++; break; } } } System.out.println(number); System.out.println((number==0) ? "no dublicate found" : "dublicate found"); } }

12th Feb 2020, 6:20 AM
Shubham
Shubham - avatar
3 Respuestas
+ 2
Thnx...
12th Feb 2020, 7:18 AM
Shubham
Shubham - avatar
+ 1
for(int j = 1; j < str1.length(); j++) <j> always initialize with a value of 1. for(int j = i + 1; j < str1.length(); j++) <j> initialize dynamically. Value of <j> depends on value of <i>.
12th Feb 2020, 7:01 AM
Ipang
0
Welcome ...
12th Feb 2020, 7:19 AM
Ipang