从char数组中提取正数和负数

时间:2014-11-27 08:27:49

标签: java

我试图从字符数组(来自文件)中提取正数和负数。显然我写的方法只适用于正数。任何人都可以告诉我如何修改这个来处理负数? 所以我要说我的角色数组是

{'1',' ','2','-','2',' ','3','4'}

它应该输出一个包含

的列表

{1,2,-2,34}

我们将不胜感激。 提前致谢

我的方法是:

private static ArrayList<Integer> extractIntegers(char[] characters) 
{
    int beginning = -1;
    ArrayList<Integer> numbers = new ArrayList<Integer>();
        for (int i = 0; i < characters.length; i++)
            {
                boolean isNumber = Character.isDigit(characters[i]);
                    if (beginning != -1 && !isNumber) {
                        numbers.add(parseIntegers(characters, beginning, i));
                        beginning = -1;
                    } else if (beginning == -1 && isNumber) {
                        beginning = i;
                    }
            }
                    if (beginning != -1) {
                        numbers.add(parseIntegers(characters, beginning, characters.length));
                    }
    return numbers;
  }

/*Parses characters to Integers*/
private static int parseIntegers(char[] characters, int begin, int end) {
    return Integer.parseInt(new String(characters, begin, end - begin));
  }

3 个答案:

答案 0 :(得分:2)

尝试使用类似的功能:

private static ArrayList<Integer> extractIntegers(char[] characters) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    char lastValue = 49;//let it be int 1
    for (int i = 0; i < characters.length; i++) {
        if (characters[i] == ' ') {
            continue;
        }
        boolean isNumber = Character.isDigit(characters[i]);//is really a digit?
        boolean isSign = (lastValue == '-');//is last char value that we read really a '-'
        if (isNumber) {
            if (!isSign)
                numbers.add(((int) characters[i] - 48));
            else
                numbers.add((int) -1 * ((int) characters[i] - 48));//if last token was "-" multiply by -1
        }
        lastValue = characters[i];
    }
    return numbers;
}

答案 1 :(得分:0)

Replace for loop in your code and try with this:

         for (int i = 0; i < characters.length; i++) {
                boolean isNumber = Character.isDigit(characters[i]);
                if(!isNumber) {
                     if(characters[i]=='-') {
                         i++; // to read next digit
                         if(i<characters.length &&  Character.isDigit(characters[i])) {
                         numbers.add(Integer.parseInt((String.valueOf(characters[i])))*-1);
                     }
                     }                   
                }
                else {
                    numbers.add(Integer.parseInt((String.valueOf(characters[i]))));
                }
            }

答案 2 :(得分:0)

    private List<Integer> extractIntegers(char[] characters) throws Exception 
    {
        List<Integer> numbers = new ArrayList<Integer>();
        for (int i = 0; i < characters.length; i++)
        {
            boolean isNumber = Character.isDigit(characters[i]);
            if (isNumber) {
                numbers.add(Character.getNumericValue(characters[i]));
            } else if (characters[i] == '-' && i+1 < characters.length && Character.isDigit(characters[i+1])) {
                i++;
                numbers.add(Character.getNumericValue(characters[i]) * -1);
            } else {
                throw new Exception("Corrupted char array");
            }
        }
        return numbers;
     }

e.g

char [] chars = {'1','2',' - ','5'}; - &GT; 1 2 -5

char [] chars = {'1','2',' - '}; - &GT;例外

char [] chars = {'1','2',' - ',' - ','6'}; - &GT;例外

char [] chars = {'1','2','1','a','6'}; - &GT;例外