
StringIndexOutOfBounds Exception in flames program

Hi Friend,
We have providing you a program. Try it:
import java.util.*;
public class FlamesProgram{
static String name1 = "";
static String name2 = "";
public void setNames( String name1, String name2 ){
this.name1 = name1.toLowerCase().trim().replaceAll( " ", "" );
this.name2 = name2.toLowerCase().trim().replaceAll( " ", "" );
}
private String find(){
String difference = "";
String temName = this.name2;
for( int i=0; i<name1.length(); i++ ){
String a = name1.charAt(i) + "";
if( name2.indexOf( a ) != -1 ){
name2 = name2.replaceFirst( a, "" );}
else{difference += a;}
}
difference += name2;
this.name2 = temName;
return difference;
}
public String getFLAMES(){
String flamesResult = "";
int difference = find().length();
if( difference == 0 ){
if( name1.equals( name2 ) )
return "Names are same";
else
return "Error in finding flame";
}
String fs[] = { "FRIEND", "LOVE", "AFFAIR", "MARRIAGE", "ENEMY", "SISTER/BROTHER" };
int k=0;
for( int i=5; i>0; i-- )
{int j=1;
while( j < difference ){
k++;
if( k == fs.length )
k = 0;
j++;
}
String fs1[] = new String[i];
int m = 0;
for( int l=0; l<fs.length; l++ )
if( l != k )
fs1[m++] = fs[l];
fs = fs1;
if( k == fs.length )
k = 0;
flamesResult = fs[0];
}
return flamesResult;
}
public static void main(String[] args){
Scanner input=new Scanner(System.in);
FlamesProgram fl = new FlamesProgram();
System.out.println("Enter Name 1: ");
name1=input.nextLine();
System.out.println("Enter Name 2: ");
name2 = input.nextLine();
fl.setNames( name1, name2 );
System.out.println( "Your Relationship is : "+ fl.getFLAMES() );
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.