Java Substring返回0

时间:2013-12-02 08:17:53

标签: java android stringbuilder

我正在开发一个数字命理应用..其中..三个编辑文本用于收集用户的名字,中间名和姓氏..在这些值中,只有名字的第一个数字被取而且所有这些值都被追加使用字符串构建器..并显示计算结果..如果用户输入所有名字,中间名和姓氏,应用程序正常工作,如果对某些人没有中间名,那么当他们进行计算时,应用程序是崩溃..请帮助..我正在下面的代码..

MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    public void gReport(View V) 
    {
        EditText et1 = (EditText) findViewById (R.id.editText1);
        EditText et2 = (EditText) findViewById (R.id.editText2);
        EditText et3 = (EditText) findViewById (R.id.editText3);
        TextView tv1 = (TextView) findViewById (R.id.textView1);

        long sum2 = 0;

        String firstName = et1.getText().toString();
        String middleName = et2.getText().toString();
        String lastName = et3.getText().toString();
        String aChar1 = firstName.substring(0,1);
        String aChar2 = middleName.substring(0,1);
        String aChar3 = lastName.substring(0,1);

        StringBuilder sb= new StringBuilder();
        sb.append(aChar1);

        sb.append(aChar2);
        sb.append(aChar3);
        String aChar = sb.toString();
        sum2 = getSum(String.valueOf(aChar));


        tv1.setText(String.valueOf(aChar));
        Intent in = new Intent(this, FirstActivity.class);
        in.putExtra("name2",sum2 + "");
        startActivity(in);


    }




    private long getSum(String text) {
        // TODO Auto-generated method stub
        long sum2 = 0;
        char[] name2 = new char[text.length()];

               name2 = text.toCharArray();

               for(int i=0; i<text.length(); i++)
               {
                   sum2 += value2( name2[i] );
                }
                 while (sum2>9)            

               {              

                   sum2 = findDigitSum2(sum2);

               }
        return sum2;
    }

    private long findDigitSum2(long n) {
        // TODO Auto-generated method stub
        int sum2=0;
        while (n != 0) 
        {
         sum2 += n % 10;
         n = n / 10;
        }
        return sum2;
    }

    private long value2(char a) {
        // TODO Auto-generated method stub
        switch(a)
        {
           case 'A':return 1;    
           case 'B':return 2;
           case 'C':return 3;
           case 'D':return 4;
           case 'E':return 5;
           case 'F':return 6;
           case 'G':return 7;
           case 'H':return 8;
           case 'I':return 9;
           case 'J':return 1;
           case 'K':return 2;
           case 'L':return 3;
           case 'M':return 4;
           case 'N':return 5;
           case 'O':return 6;
           case 'P':return 7;
           case 'Q':return 8;
           case 'R':return 9;
           case 'S':return 1;          
           case 'T':return 2;
           case 'U':return 3;
           case 'V':return 4;
           case 'W':return 5;
           case 'X':return 6;
           case 'Y':return 7;
           case 'Z':return 8;
           default:return 0;

        }
    }

FirstActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstactivity_xm);







        TextView tv2 = (TextView) findViewById (R.id.textView2);

        tv2.setText(getIntent().getStringExtra("name2"));


}
}

2 个答案:

答案 0 :(得分:0)

它可能会崩溃,因为中间名字段为空,长度为0,并且您尝试从非现有索引获取字符。 在使用substring方法之前,您可以使用isEmpty()检查middleName字符串是否为空或至少有1个字符

例如:

String aChar2 = ""; 
if (!middleName.isEmpty())
{
   aChar2 = middleName.subString(0,1);
}

答案 1 :(得分:0)

可能是因为以下几行:

String aChar2 = middleName.substring(0,1);

如果middleName为空,则会抛出IndexOutOfBoundsException(请参阅this link)

要么捕获此异常,要么更好,在执行subString之前检查String是否为空。