破坏的charAt()循环

时间:2013-12-16 18:39:44

标签: java charat

我被要求做以下事情:

  

更改介绍性问题1以按姓氏的字母顺序显示输入的名称。      问题1:编写一个创建5 x 2阵列的程序。第一列表示名字,第二列表示姓氏。让用户输入5个完整(第一个和最后一个)名称以存储在此2D阵列中。完成所有输入后,显示名称,但将其显示为LastName,首先是初始名称。

我现在意识到下面概述的方法并不是攻击这个程序的正确/最有效的方法,但我仍然感到困惑的是为什么程序失败了。当我在eclipse中运行程序时,它会打印一行问号,然后抛出以下异常:

  

线程“main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0    在java.lang.String.charAt(未知来源)   在TwoD6.main(TwoD6.java:36)

它似乎没有运行第二时间,我无法理解为什么会这样。我尝试在变量类型之间进行更改,担心不接受char的数字值(charAt命令最初在if语句中),以及在用户执行之前填充数组(想想也许是错误来自java不接受字符串在位置0处有字符。


import java.util.Scanner;

public class TwoD6 {    

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Input 5 full names (first and last).");
    String [][] d2 = { { " "," " },
            {" "," "},
            {" "," "},
            {" "," "},
            {" "," "} };
            //new String[5][2]; initial string declaration, which I previously thought could be the source of the issues
    for(int i = 0; i < d2.length; i++)
    {
        d2 [i][0] = sc.next();
        d2 [i][1] = sc.next();
    }

    for(int i = 0; i < d2.length-1; i++)
    {
        char c = d2[i][1].charAt(0);
        char d = d2[i+1][1].charAt(0);

        int e = c; int f = d;
        if(e < f)
        {
            System.out.println("????????????????"); //implemented to see how many times the loop runs; currently prints once
            String a = ""; String b = ""; //switchers
            d2[i][0] = a; d2[i][1] = b;
            d2[i][0] = d2[i+1][0]; d2[i+1][0] = a;
            d2[i][1] = d2[i+1][1]; d2[i+1][1] = b;
        }
    }
    for(int i = 0; i < d2.length; i++) //printing the array
    {
        char firstChar = d2[i][0].charAt(0); 
        System.out.println(d2[i][1] + ", " + firstChar + ".");
    }

 }

} 

我很感激你能给我的任何解释。我对确定问题集的可行解决方案并不感兴趣,因为我已经编写了一个更好的程序,但是我想知道charAt()命令出了什么问题。如何解决下次的问题。谢谢!

3 个答案:

答案 0 :(得分:0)

如果没有实际的输入数据,我们只能猜测。

乍一看,有问题的代码行似乎是

char firstChar = d2[i][0].charAt(0); 

在上一个for循环中。

显然发生错误的字符串为空,因此尝试调用charAt(0)(即尝试读取第一个字符)会抛出StringIndexOutOfBoundsException,因为空字符串没有任何字符读取。

换句话说,您可能没有提供足够的输入,因此d2数组未填满,而是将一些值设置为空字符串。

答案 1 :(得分:0)

第二个“for”循环中“if-block”中的代码将在2-d数组的下一个索引处存储零长度字符串,这将在下一次迭代中导致异常

真正的问题是在一组行之后(第二个是循环的if块)

   if(e < f)
    {
        System.out.println("????????????????"); //implemented to see how many times the loop runs; currently prints once
        String a = ""; String b = ""; //switchers
        d2[i][0] = a; d2[i][1] = b;
        d2[i][0] = d2[i+1][0]; d2[i+1][0] = a;
        d2[i][1] = d2[i+1][1]; d2[i+1][1] = b;
    }

最精确的是

         d2[i][0] = a; d2[i][1] = b;
         d2[i+1][0] = a;  // THIS ONE WILL CAUSE PROBLEM IN NEXT ITERATION
         d2[i+1][1] = b; // THIS ONE WILL CAUSE PROBLEM IN NEXT ITERATION

因为a =“”,b =“” d2 [i + 1] [0]和d2 [i + 1] [1]的值只是零长度字符串。在循环中的下一个索引处没有任何字符

您可以通过在代码的第二个开头和结尾添加以下行来测试自己,并查看输出

 System.out.println(i+"  "+d2[i][0]+" ---- "+d2[i+1][0]);

这是我得到的

   Input 5 full names (first and last).
   as gh
   qe fg
   er ty
   df gr
   yp dfg
   0  as ---- qe
   0 as ---- qe   //end of 0th loop
   1  qe ---- er
   ???????????????? //if block executed in 1st loop
   1 er ----     // end of loop notice value at index 2 is blank now
   2   ---- df   // Now we'll have exception in this line
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:686)
    at javaapplication7.Main.main(Main.java:33)

[编辑] 此代码也无法解决您在修复此错误后尝试解决的问题。由于此代码存在许多逻辑错误

答案 2 :(得分:0)

错误表示您正在尝试访问字符串范围之外的字符。如果字符串的大小为0(例如:空字符串),则可以这样做。 charAt(0)会因此失败,因为它会尝试访问第一个不存在的字符。

一般情况下,如果你索引到一个字符串,但不能保证字符串是否为空,那么处理它的一种方法是在尝试访问之前首先检查它是否是“有效”字符串人物。例如

// Check if it actually has characters before accessing it
if (d2[i][0].length() > 0) {
   //access the string
}