0

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;
    }
}
4
  • 6
    If you're guaranteed whitespace, why not just split on whitespace? Also, you could use a switch. And check all the conditions at once, since they all do precisely the same thing. Also, pretty sure currentNumber += inputChar isn't what you want. Commented Jan 9, 2012 at 14:52
  • "I want to check character by character" What is the ultimate purpose of doing so? Is this an academic exercise, homework? If not, note that the ECMAScript (JavaScript) version of the ScriptEngine can evaluate string expressions. Commented Jan 9, 2012 at 14:57
  • I'm not guarenteed whitespace, I wan't it to accept either. Thanks Commented Jan 9, 2012 at 15:00
  • BTW: Are you missing the [homework] tag? Commented Jan 9, 2012 at 15:06

4 Answers 4

1
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

Sign up to request clarification or add additional context in comments.

3 Comments

This will drop the last string if its a number.
Ah, yes, missed that.. After the for loop, check the length of currentNumber. If it's > 0, add it to the list..
You should really check before each symbol as well. e.g. 5 * -3
1

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

Comments

1

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);

3 Comments

Same issue with currentNumber += inputChar.
@DaveNewton, Fair point, I didn't want to be writing all his program for him. ;)
I've always thought homework answers should employ a buginator, just not sure how to work it out.
0

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).

1 Comment

You can not cast a char to a String.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.