Rachit Bhargava输出错误不正确

时间:2014-05-08 12:27:59

标签: java bluej

我制作了一个问题计划:

用Java编写程序输入名字,中间名和姓氏。验证包含名字和姓氏的数字签名。如果签名不包含任何这些,请打印"无效签名"。另外,用一行打印他的全名。

程序是:

// 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+".");
    }
}

输出结果为:

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.

请注意:我使用BlueJ 3.1.1制作我的Java程序。任何帮助将不胜感激。

等待某人的帮助...

2 个答案:

答案 0 :(得分:0)

if((signature.substring(i, (i+(lf-1)))).equals(fname))行,endIndex必须大于String signature的长度。

答案 1 :(得分:0)

错误意味着“您已尝试查看字符串的第17个字符,但字符串少于17个字符”。

我建议使用调试程序逐步执行代码 - 找出字符串内容失败时的内容,以及计算数字16的方式。