+ 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); } }

19th May 2020, 5:22 AM
Shivam Rawal
2 odpowiedzi
+ 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... 👍
19th May 2020, 5:37 AM
sarada lakshmi
sarada lakshmi - avatar
+ 1
Corrected mistake
19th May 2020, 5:46 AM
Shivam Rawal