本文介绍了VariableDeclaratorID例外,在java中while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经全局声明一个String数组。而在while循环的方法里面,当我试图赋予一定的价值,它显示VariableDeclaratorID预计此令牌后。

  //全局声明
字符串名称[];而(scanner.hasNextLine()){
字符串输入= scanner.nexLine();
命名[] = input.split(,); //这里的错误 - 令牌]语法错误,VariableDeclaratorId预计此令牌后
}

请帮我解决这个问题。


解决方案

  NAME = input.split();

通过调用每一个你,你将覆盖阵列的每一次的内容在文件中读取行上述行。如果这不是你想要做的话,我建议使用一个ArrayList(这将在大小动态缩放)的东西。

 的ArrayList<的String []> NAME =新的ArrayList<的String []>();

然后只需拨打

  name.add(input.split());

从文件中读取每一行。

I have declared a String array globally. And inside a method in while loop when i tried to assign some value, it shows VariableDeclaratorID expected after this token.

//global declaration
String name[];

While (scanner.hasNextLine()){
String input = scanner.nexLine();
name[] = input.split(","); //here error - Syntax error on token "]", VariableDeclaratorId expected after this token
}

Please help me to resolve this issue.

解决方案
name = input.split(",");

By calling the above line for each line you read in the file you will be overwriting the contents of your array each time. If this is not what you want to do then I suggest using an ArrayList (which will scale in size dynamically).

ArrayList<String[]> name = new ArrayList<String[]>();

Then simply call

name.add(input.split(","));

for each line read from file.

这篇关于VariableDeclaratorID例外,在java中while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 00:48