import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
/**
This class prints the numeric value of a letter grade given by the user.
*/
public class Words
{
int i=0;
String[] wordz;
/**
Constructs words class
*/
public Words()
{
wordz= new String[5];
}
/**
collects 5 words from user and places then into array
@return the gradeValue
*/
public void inputArray(String a, String b, String c, String d, String e)
{
wordz = new String[] { a, b, c, d, e };
}
/**
counts even and odds
@return numeric grade
*/
public void removeShortWords()
{
ArrayList<String> wordzList = new ArrayList<String>(Arrays.asList(wordz));
for(i=0; i < wordz.length; i++)
{
if(wordz[i].length() < 3)
wordzList.remove(i);//out of bounds error here
String[] wordz = wordzList.toArray(new String[wordzList.size()]);
}
}
/**
prints out the array of 10 positive integers
@return numeric grade
*/
public void printArray()
{
System.out.println(Arrays.toString(wordz));
}
}
这是我的测试人员类。字数组程序的出错错误
import java.util.Scanner;
public class WordPrgm {
public static void main(String[] args)
{
Words wordArray = new Words();
System.out.println("PLease enter five words");
Scanner in = new Scanner(System.in);
String w1 = in.nextLine();
String w2 = in.nextLine();
String w3 = in.nextLine();
String w4 = in.nextLine();
String w5 = in.nextLine();
wordArray.inputArray(w1, w2, w3, w4, w5);
wordArray.removeShortWords();
wordArray.printArray();
}
}
这里的程序应该从数组中删除少于3个字母的单词并打印出新单词。我一直在一遍又一遍地查看代码,但是我看不到解决方案在哪里以及我错过了什么。我认为for循环可能会搞砸了。谢谢!
我在程序的这一点上总是收到一个错误。
wordzList.remove(i);
少于五个?看起来更像我三个人。 – laune 2014-12-05 07:09:00
是的,对不起,我没有想到,它应该少于3,而不是5。 – javaProgrammer 2014-12-05 07:12:05
@javaProgrammer如果答案帮助你不要忘记接受它 – 2014-12-19 07:40:23