+ 2
How this code is giving int output
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String n =sc.nextLine(); int a =n.length(); String w=""; char [] x = {'G','
#x27;,'T'}; for(int j=0;j<a;++j){ for(int i=0;i<2;++i){ if(n.charAt(j)==x[i]){ w = w + j; } } } int s = w.indexOf("quot;); System.out.print(w); } }2 Antworten
+ 1
Once check the line w=w+j
You are adding integer(index)
Suppose n=$arada, index of $ is 0
So, w=w+0 (since, $==$)... At this time the value of j is 0...so,w=w+0 = 0(which is in string format) and you are getting the result as 0 (which you are thinking the result is in integer format). If you want a string, then try with arrays... Hope you understand... 👍
+ 1
Corrected mistake