【发布时间】:2014-10-15 21:37:43
【问题描述】:
我正在尝试创建一个接受以下形式的字符串的方法: “某事 a:某事 b:某事 c”
我需要忽略冒号,并将各种东西存储为变量(私有)。
我有我的 getter 和 setter,还有我的 toString。但是,我在创建我的 for 循环和在其中创建适当的子字符串以创建我的三个单独的变量(a、b、c)时遇到问题。我也对该方法如何从命令行接收输入有疑问。
这是我目前的代码
public void createList(String a) {
//creates an arrayList to store the variables
int index = 0;
int i = a.indexOf(':'); //uses the indexOf(':') as the start/end of the substring
for(int j = 0, j < a.length(); j++){
xxxx = a.substring(0 , i - 1); //something a
yyyy = a.substring(i + 1, i - 1); //something b
zzzz = a.substring(i + 1, a.length() - 1); // something c
}
我有一个单独的 main 方法,它接受来自命令行的输入(.txt 文件)。
【问题讨论】:
-
使用
a.split(" : ")
标签: java for-loop methods substring