0

string index out of bound exception

import java.util.*; import java.lang.*; import java.io.*; public class pali { public static void main (String[] args) { Scanner sc= new Scanner(System.in); int test_cases= sc.nextInt(); for(int i= 0 ; i< test_cases;i++) { int len= sc.nextInt(); String str=sc.nextLine(); String p = ""; for(int j=0;j<len;j++) p= str.charAt(j)+p; System.out.println("p"); if(p.equals(str)) System.out.println("Yes"); else System.out.println("No"); } } } /* Exception in thread "main"// java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at gfg_pali.main(File.java:14) */

11th Jul 2018, 7:09 PM
Smriti Rastogi
Smriti Rastogi - avatar
2 ответов
+ 3
int len = str. length();
11th Jul 2018, 8:27 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 2
Instead of relying on your user to input the length of the string, it's probably best to get the length programmatically.
11th Jul 2018, 7:12 PM
Keto Z
Keto Z - avatar