I am trying to write a code but facing a java.lang.StringIndexOutOfBoundsException. Please check the code out

Aanjaneya Shah :

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. See Sample explanation for more details.

Input : First line, contains an intger 'T' denoting no. of test cases. Each test consists of a single line, containing two space separated strings S1 and S2 of equal length.

Output:

For each test case, if any of the permutation of string S1 is equal to the string S2 print YES else print NO.

Constraints:

1<= T <=100

1<= |S1| = |S2| <= 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");  
            }  
    }  
    }
}

java.lang.StringIndexOutOfBoundsException the error
where the issue persists issue

Elliott Frisch :

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.

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.

int n=sc.nextInt();
sc.nextLine(); // <-- skip trailing new line.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I have been getting "Failed to write to 'index.js'. Please try again." whenever I try to update my lambda function code. why am I facing this issue?

I am unable to solve this :java.lang.StringIndexOutOfBoundsException: String index out of range: 54

I am trying to create circle avatar in Flutter. I have tried this but not giving me circle please check the code bellow:

i was trying to create a modal, please check my code

Can someone please help me with the code I am writing? (Java)

I am unable to Decrypt my text but I am able to encrypt can someone please check my code

i am trying to create foreign keys but i got error 1822 .. please see my code below

I am trying to do a project on GTK+ using Glade but when I am compiling my code and running the executable file I am facing some errors

Leap days calculator code error, Please point out where I am going wrong

Today I am facing very abnormal bug in android code

What is the logical error I am facing in Pascal's Triangle code?

Hi! I am trying to center this code but it is not centering. I can not figure out what I did wrong

Why am I getting a java.lang.StringIndexOutOfBoundsException error when I run?

i am getting java.lang.NullPointerException when trying to check internetConnection?

I am trying to understand the following javascript code

I am trying the following code , but it is throwing exception

I am trying to find the problem within this code

I am trying to finish this code for this menu and cant figure out why it won't loop correctly?

I am trying to write a Python code that reads each item in a list and add and find the average of that list with certain conditions

I am trying to write a simple asynchronous code in JS, but it doesn't seem to work

I am trying to write a code in python to get the values from a multiline string and store it in dictionary

I am trying to write a code that checks if a number between 2 and 1000 is a prime Number

I am trying to search user by name, and load some data to Recycleview. But the code is not working. Can somebody help me please

Im Trying To Run Python On Visual Studio Code But am Facing An File Not Found Error

I am trying to check that If email already exist in databse or not or not in database but my code throws error

I'm getting java.lang.StringIndexOutOfBoundsException: String index out of range: -1

Why doesn't following java code throw java.lang.StringIndexOutOfBoundsException when there is no element present at index 1?

I have this python code that I am trying to turn into javascript but I cant seem to figure out which if statement. to use

I am trying to run XGBoost in R but am facing some issues