-3

I have made a program for question:

Write a program in Java to input first name, middle name and last name. Validate the digital signature that contains first name and last name. If the signature doesn't contain any of these, print "Invalid Signature". Also, print his full name in a single line.

The programs is:

// Write a program in Java to input first name, middle name and last name. Validate the digital signature that contains first name and last name. If the signature doesn't contain any of these, print "Invalid Signature". Also, print his full name in a single line.
import java.io.*;
public class TaskPE_2
{
    public static void main(String args[])throws IOException, StringIndexOutOfBoundsException
    {
        BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please enter your full name");
        String name=read.readLine();
        System.out.println("Please enter your digital signature");
        String signature=read.readLine();
        String fname="", mname="", lname="";
        char n='0';
        name=name.trim();
        String dummy=name;
        dummy+=" ";
        while(n!=' ')
        {
            n=dummy.charAt(0);
            if(n!=' ')
                fname+=n;
            dummy=dummy.substring(1);
        }
        n='0';
        while(n!=' ')
        {
            n=dummy.charAt(0);
            if(n!=' ')
                mname+=n;
            dummy=dummy.substring(1);
        }
        n='0';
        while(n!=' ')
        {
            n=dummy.charAt(0);
            if(n!=' ')
                lname+=n;
            dummy=dummy.substring(1);
        }
        int l=name.length();
        int namef=0, namel=0;
        dummy=name;
        int lf=fname.length(), ll=lname.length();
        int i;
        for(i=0; i<=(l-(lf+1)); i++)
            if((signature.substring(i, (i+(lf-1)))).equals(fname))
                namef=1;
        if(namef==1)
            System.out.println("F PASSED");
        for(i=0; i<=(l-(ll+1)); i++)
            if((signature.substring(i, (i+(ll-1)))).equals(lname))
                namel=1;
        if(namel==1)
            System.out.println("L PASSED");
        if(namef==1&&namel==1)
            System.out.println("Your entered digital signature is valid");
        else
            System.out.println("Invalid digital signature entered");
        System.out.println("Your full name is "+fname+" "+mname+" "+lname+".");
    }
}

The output is:

Please enter your full name
Rachit R Bhargava
Please enter your digital signature
Rachit Bhargava
Invalid digital signature entered
Your full name is Rachit R Bhargava.

Please Note: I am using BlueJ 3.1.1 to make my Java programs. Any help would be appreciated.

Waiting for someone's help...

Rachit Bhargava
  • 160
  • 1
  • 12

2 Answers2

0

At the line if((signature.substring(i, (i+(lf-1)))).equals(fname)), the endIndex must be greater then the length of String signature.

earthmover
  • 4,039
  • 10
  • 42
  • 73
0

The error means "You have tried to look at the 17th character of a string, but the string has fewer than 17 characters".

I suggest using a debugger to step through your code - find out what the content of the string is at the point it fails, as well as how you have calculated the number 16.

slim
  • 36,139
  • 10
  • 83
  • 117