Coolmurr
Coolmurr

Reputation: 65

Java .add(value) with lists

Basically I've got an input string, my test string is '5 + 4' and I want to check character by character to create a list in the form [5,+,4], ie white spaces are ignored. Also, if my test string was '567+5-1' it would output [567,+,5,-,1] by concatenating the numbers together. Unfortunately, it won't let me do .add(inputChar) to my returnValue, saying symbol not found... any ideas?

import java.util.*;

public class CharacterArray {
    public List<String> splitToChar(String s) {
        List<String> returnValue = new LinkedList<String>();
        char[] chars = s.toCharArray();
        System.out.println(chars);
        int currentNumber;
        for (char inputChar : chars) {
            if (Character.isDigit(inputChar) == true) {
                currentNumber += inputChar;
            } else if (inputChar == '.') {
                currentNumber += inputChar;
            } else if (inputChar == '+') {
                returnValue.add(inputChar);
            } else if (inputChar == '-') {
                returnValue.add(inputChar);
            } else if (inputChar == '/') {
                returnValue.add(inputChar);
            } else if (inputChar == '*') {
                returnValue.add(inputChar);
            } else if (inputChar == '(') {
                returnValue.add(inputChar);
            } else if (inputChar == ')') {
                returnValue.add(inputChar);
            } else {
                System.out.println("Incorrect input symbol");
            }
        }
        return returnValue;
    }
}

Upvotes: 0

Views: 6518

Answers (4)

Gijs Overvliet
Gijs Overvliet

Reputation: 2691

import java.util.*;

public class CharacterArray {
    public List<String> splitToChar(String s) {
        List<String> returnValue = new LinkedList<String>();
        char[] chars = s.toCharArray();
        System.out.println(chars);
        String currentNumber = "";
        for (char inputChar : chars) {
            if (Character.isDigit(inputChar) == true) {
                currentNumber += inputChar;
            } else if (inputChar == '.' ||
                    inputChar == '+' ||
                    inputChar == '-' ||
                    inputChar == '/' ||
                    inputChar == '*' ||
                    inputChar == '(' ||
                    inputChar == ')') {
                if (currentNumber.length() > 0){
                    returnValue.add(currentNumber);
                }
                currentNumber = "";
                returnValue.add(""+inputChar);
            }  else {
                System.out.println("Incorrect input symbol");
            }
        }
        if (currentNumber.length() > 0){
            returnValue.add(currentNumber);
        }
        return returnValue;
    }
}

By the way, your currentNumber should be a String

Upvotes: 1

murgatroid99
murgatroid99

Reputation: 20277

Your returnValue is a List<String> but you are trying to add a char to the list. You can fix this by converting inputChar to a String when adding it to the list by using String.valueOf(inputChar).

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533530

You can't add a char to a List<String> perhaps what you are trying to do is like

String currentNumber = "";
for (char inputChar : chars) {
    if (Character.isDigit(inputChar) || inputChar == '.') {
        currentNumber += inputChar;
    } else if ("+-/*()".indexOf(inputChar) >= 0) {
        if (currentNumber.length() > 0) returnValue.add(currentNumber);
        currentNumber = "";
        returnValue.add("" + inputChar);
    } else if (inputChar != ' ') {
        System.out.println("Incorrect input symbol '"+inputChar+"'");
    }
}
if (currentNumber.length() > 0) returnValue.add(currentNumber);

Upvotes: 1

David Soroko
David Soroko

Reputation: 9086

how about .add(String.valueOf(inputChar)) ?

Upvotes: 1

Related Questions