检索字符串的ArrayList

时间:2014-11-30 19:02:06

标签: java arrays arraylist

我正在尝试完成以下任务:

  

询问用户是否有数字,如果此数字为2或更大,请将其保存为数组长度。然后要求用户根据之前(完成)编写的数字输入字符串,最后显示每个输入的字符串,然后再完成二维数组*

// (the length [z] was already defined before)

String arrayer[] = new String[z];
for(multi=0; multi<arrayer.length; multi++){

    do {
        out.print("\n");
        out.print("Enter the name of [Subject "+(multi+1)+"] : ");
        try {
            arrayer[multi]=LeerTeclado.nextLine();  
            if (arrayer[multi].length()>15){
                out.println("\n-------------------------------------------------------");
                out.println("Name of subject should be max. 15 characters long");
                out.println("-----------------------------------------------------");
                continue;
            }
            break;
        } catch (InputMismatchException ex) {
            out.println("Subject name should be shorter than 15 characters\n");
            LeerTeclado.next();
        }
    } while ( true );   
}

// Where `arrayer[z].length` should display the **name** of the subject and increase depending on the number of subjects entered

int[][] mat;
mat=new int[z][n];

for (int u=0; u < arrayer.length ; u++) {
    for (int f=0; f < arrayer.length; f++) {
        out.print ("\nWrite your average grade of " + arrayer[z].length + ": ");
        mat[u][f] = LeerTeclado.nextInt();
    }       
}       

1 个答案:

答案 0 :(得分:0)

假设arrayerString个对象的数组,它们是主题的名称,只需使用out.print("\nWrite your average grade of " + arrayer[f] + ": ");,即不使用length()方法。

此外,z似乎无法定义,因此您需要定义它或使用外部循环变量之一u或{ {1}},正如我在上面的行中所做的那样。