-1

I need help separating a string into separate variables. I am trying to make a program that will get user input using nextLine() then i would need to separate the characters in that string.

For example the user could input the line 23plus or 2 3 plus.

Ive read about .split but that would only work when there are spaces in the input. I could put say that the first number is charAt(0) but how would i get the rest?

  • How can your program distinguish between `23plus` meaning `23 plus` and `23plus` meaning `2 3 plus`? – gzm0 Apr 01 '13 at 21:47
  • This doesn't make sense. Given the input `1234plus` what does your logic do? `12 34 plus` `1 234 plus`? – Boris the Spider Apr 01 '13 at 21:48
  • 1
    You cannot split a line unless you know the general structure of the line and how you're going to split it. You need to tell us these things as well as more examples of possible input and output if we're to help. As written your question doesn't make sense, and isn't answerable. – Hovercraft Full Of Eels Apr 01 '13 at 21:48
  • its supposed to be a single digit calculator. i would take one digit and do the operation – Alex Verstivskiy Apr 01 '13 at 21:49
  • 2
    *"Ive read about .split but that would only work when there are spaces in the input"* That's not even *remotely* true: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String) You might also look at [`Pattern`](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html), or even just [`String#indexOf`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int)) and [`String#substring`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int)). – T.J. Crowder Apr 01 '13 at 21:49
  • You could probably just collect the line as a string, remove all of the whitespace and iteratively process each string element in a loop. – Robb Apr 01 '13 at 21:54
  • what would you use to get rid of the whitespaces? – Alex Verstivskiy Apr 01 '13 at 21:58

2 Answers2

1

You say, the problem is 'one digit calculator' with postfixed notation 23+ :

  • digits = digits.replaceAll( "\\\\s","" );
  • First digit: int op1 = Integer.parseInt( digits.charAt(0));
  • Second digit: int op2 = Integer.parseInt( digits.charAt(1));
  • operator : digits.charAt(2), as I suggest to use +, -, /, *, used in a switch/case
  • result: 5
Aubin
  • 14,617
  • 9
  • 61
  • 84
0

How about looping over the String char by char and checking if it's a digit:

    final List<Integer> values = new LinkedList<>();
    final StringBuilder operator = new StringBuilder();
    for (final char c : input.toCharArray()) {
        if (Character.isDigit(c)) {
            values.add(Integer.parseInt(Character.toString(c)));
        } else if (!Character.isWhitespace(c)) {
            operator.append(c);
        }
    }

Note: This will only work if the input is of the form "WdigitWdigitWoperator" where "W" is optional white space.

But I understand that that is the case from your question.

Another option is to use regex, this would look something like:

    final Pattern p = Pattern.compile("^\\s*+(?<firstDigit>\\d)\\s*+(?<secondDigit>\\d)\\s*+(?<operator>\\w++)$");

    final Matcher matcher = p.matcher(input);
    if(matcher.matches()) {
        System.out.println(matcher.group("firstDigit"));
        System.out.println(matcher.group("secondDigit"));
        System.out.println(matcher.group("operator"));
    }

If this is a homework assignment (which I suspect it is) this may not be an acceptable solution however.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166