-2
我创建了下面的Java程序,其中采用了String形式的Statement。该语句的所有单词都单独存储在数组中。如何调用主驱动程序中不存在的方法?
示例 - String statement =“hello world i love dogs”; 获取存储在数组中作为 - {你好,世界,我,爱,狗}
我写了下面的代码,但我无法检查它,因为当我调用main方法中的方法时,它不会按要求工作。
如何获得输出?
public class Apcsa2 {
/**
* @param args the command line arguments
*/
public String sentence;
public List<Integer> getBlankPositions(){
List<Integer> arr = new ArrayList<Integer>();
for (int i = 0; i<sentence.length();i++){
if(sentence.substring(i, i +1).equals(" ")){
arr.add(i);
}
}
return arr;
}
public int countWords(){
return getBlankPositions().size() + 1;
}
public String[] getWord(){
int numWords = countWords();
List<Integer> arrOfBlanks = getBlankPositions();
String[] arr = new String[numWords];
for (int i = 0; i<numWords; i++){
if (i ==0){
sentence.substring(i, arrOfBlanks.get(i));
arr[i] = sentence;
}else{
sentence.substring(i + arrOfBlanks.get(i), arrOfBlanks.get(i+1));
arr[i] = sentence;
}
}
return arr;
}
public static void main(String[] args) {
// TODO code application logic here
int[] arr = {3,4,5,2,4};
String sentence = "hello world I love dogs";
}
}
_I写了下面的代码,但我无法检查它,因为当我打电话在main方法的方法,它不作为required._工作。简单地说,制作所有你想调用'static'的方法,或者在'main'方法内创建一个'Apcsa2'类的实例并调用你想要执行的方法。 –
如果你想“分割”一个字符串,你应该看看'String'类提供的标准功能。你也可以看看这个方法的实际代码 –
感谢您的快速回复,我写了以下Apcsa2 p = new Apcsa2(); p.getWord(); System.out.print(p); 还没有发生。那么如何输入句子并获得所需的输出? –