【发布时间】:2015-01-02 21:57:07
【问题描述】:
我被要求创建一个方法,在该方法中我输入一个字符串序列并创建一个数组来存储字符串的单词。到目前为止,这就是我所拥有的:
public class Tester{
public static String[] split(String s) {
// determine the number of words
java.util.Scanner t = new java.util.Scanner(s);
int countWords = 0;
String w;
while (t.hasNext()) {
w = t.next();
countWords++;
}
// create appropriate array and store the string’s words in it
// code here
String[] words = new String[4]; // Since you are using an array you have to declare
// a fixed length.
// To avoid this, you can use an ArrayList
// (dynamic array) instead.
while (t.hasNext()) {
w = t.next();
words[countWords] = w;
countWords++;
}
return words;
}
}
现在我必须以字符串一二三四作为参数调用方法split。如果我听起来很天真,我很抱歉。我是编程新手。我一直在观看大量关于此的教程,但是当我尝试调用该方法时,我的代码不断出现红色标记。
*代码的前 14 行(从“public class”到“code here”)是给我的问题的一部分,因此不应更改它们。如有必要,您可以更改其余代码。
编辑:
如何创建一个调用 split 方法的 main 方法?这是我尝试过的:
class Demo
{
public static void main(String[]args)
{
Tester object = new Tester();
object.split(s);
System.out.println(words[i]);
}
}
基本上,我创建了另一个调用 split 方法的类。但是,我不断收到红色标记。
【问题讨论】:
-
你的问题是?
标签: java arrays eclipse string methods