0

I want to compare two Strings character by Character...Whats wrong with my code? Pls help ..

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc=new Scanner (System.in); String s=sc.nextLine (); String s1=sc.nextLine (); int l=s.length (); int i=0,count=0; Boolean flag=true; while (i <l) { if (s.charAt (i)!=s1.charAt (i)) { flag=false; break; } } System.out.print (b); } }

13th Aug 2017, 3:08 AM
Shubham Choudhary
Shubham Choudhary - avatar
2 Antworten
+ 4
You consider equals a library method, but not charAt() or nextLine()? 😜 Anywho, it's an infinite loop. (If s has a length). 'b' is also undefined, in your print statement. I recommend using a for loop instead.
13th Aug 2017, 3:13 AM
Rrestoring faith
Rrestoring faith - avatar
0
The only mistake was i hadn't increment i##Silly Me...it goes wrong while I edited it after posting here...
13th Aug 2017, 3:18 AM
Shubham Choudhary
Shubham Choudhary - avatar