[英]I am trying to write a code but facing a java.lang.StringIndexOutOfBoundsException. Please check the code out
So I am trying to take multiple inputs.所以我试图接受多个输入。 here is the question:- Given two strings of equal length, you have to tell whether they both strings are identical.
这里的问题是:- 给定两个长度相等的字符串,您必须判断它们是否相同。
Two strings S1 and S2 are said to be identical, if any of the permutation of string S1 is equal to the string S2.如果字符串 S1 的任何排列等于字符串 S2,则称两个字符串 S1 和 S2 相同。 See Sample explanation for more details.
有关更多详细信息,请参阅示例说明。
Input : First line, contains an intger 'T' denoting no.输入:第一行,包含一个表示没有的整数“T”。 of test cases.
测试用例。 Each test consists of a single line, containing two space separated strings S1 and S2 of equal length.
每个测试由一行组成,包含两个等长的空格分隔的字符串 S1 和 S2。
Output:输出:
For each test case, if any of the permutation of string S1 is equal to the string S2 print YES else print NO.对于每个测试用例,如果字符串 S1 的任何排列等于字符串 S2,则打印 YES 否则打印 NO。
Constraints:约束:
1<= T <=100 1<= T <=100
1<= |S1| 1<= |S1| = |S2|
= |S2| <= 10^5
<= 10^5
String is made up of lower case letters only.字符串仅由小写字母组成。
import java.io.*;
import java.util.*;
public class test
{
public static void main(String args[])throws IOException
{
String parts[]=new String[10];
Scanner sc=new Scanner(System.in);
System.out.println("Enter no of inputs");
int n=sc.nextInt();
int j=n;
for(int i=0;i<j;i++)
{
System.out.println("Enter string");
String s=sc.next();
int in=s.indexOf(" ");
String s1=s.substring(0,in);
String s2=s.substring(in+1);
boolean status = true;
if (s1.length() != s2.length())
{
status = false;
}
else
{
char[] ArrayS1 = s1.toCharArray();
char[] ArrayS2 = s2.toCharArray();
Arrays.sort(ArrayS1);
Arrays.sort(ArrayS2);
status = Arrays.equals(ArrayS1, ArrayS2);
}
if (status==true)
{
System.out.println(s1 + " and " + s2 + " are anagrams");
}
else
{
System.out.println(s1 + " and " + s2 + " are not anagrams");
}
}
}
}
the error
错误
issue
问题
The issue is the statement问题是声明
String s = sc.next();
That will never consume a token with a space (because white space is the delimiter).这永远不会消耗带有空格的标记(因为空格是分隔符)。 Change it to
将其更改为
String s = sc.nextLine();
And then进而
int in = s.indexOf(" ");
will work (but only if the input contains a space);将工作(但仅当输入包含空格时); so you should still test it is not
-1
yourself.所以你仍然应该自己测试它不是
-1
。
if (in >= 0) {
String s1 /* ... */
}
Also,还,
int n=sc.nextInt();
will leave trailing newlines (and skip your next input) using nextLine()
so consume that too.将使用
nextLine()
留下尾随的换行符(并跳过您的下一个输入nextLine()
因此也使用它。
int n=sc.nextInt();
sc.nextLine(); // <-- skip trailing new line.
Change your code as follows:更改您的代码如下:
System.out.print("Enter no of inputs: ");
int n = Integer.parseInt(sc.nextLine());
for (int i = 0; i < n; i++) {
System.out.print("Enter string: ");
String s = sc.nextLine();
...
...
...
}
A sample run:示例运行:
Enter no of inputs: 3
Enter string: Hello Hi
Hello and Hi are not anagrams
Enter string:
I recommend you go through Scanner is skipping nextLine() after using next() or nextFoo()?我建议您在使用 next() 或 nextFoo() 后查看Scanner 是否跳过 nextLine()? for a better understanding of
Scanner
.为了更好地了解
Scanner
。
Try this:尝试这个:
System.out.println("Enter string");
sc.nextLine();
String s = sc.nextLine();
Try below code.试试下面的代码。 It print YES and NO according to your requirement.
它根据您的要求打印 YES 和 NO。
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
// Main Class
public class ThreadGroupDemo implements Runnable {
public void run() {
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args) throws IOException {
String parts[] = new String[10];
Scanner sc = new Scanner(System.in);
System.out.println("Enter no of inputs");
int n = Integer.parseInt(sc.nextLine());
int j = n;
for (int i = 0; i < j; i++) {
System.out.println("Enter string");
String s = sc.nextLine();
int in = s.indexOf(" ");
String s1 = s.substring(0, in);
String s2 = s.substring(in + 1);
boolean status = true;
if (s1.length() != s2.length()) {
status = false;
} else {
char[] ArrayS1 = s1.toCharArray();
char[] ArrayS2 = s2.toCharArray();
Arrays.sort(ArrayS1);
Arrays.sort(ArrayS2);
status = Arrays.equals(ArrayS1, ArrayS2);
}
if (status == true) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}
I hope it helps.我希望它有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.