0

I encountered a problem while converting a string to char array... my problem is:-

String str="2+32*2";  
char a[]=new char[str.length()];  
str.getChars(0,str.length,a,0);  

my array looks like this:-

a[0]='2';  
a[1]='+';  
a[2]='3';  
a[3]='2';  
a[4]='*';  
a[5]='2';  

but I need that array to look like this:-

a[0]='2';  
a[1]='+';  
a[2]='32';  
a[3]=' *';  
a[4]='2';  

What do I have to do? somebody please help!!!!

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276

5 Answers5

2

You can't do that because '32' isn't a char, but two chars.

You can create an ArrayList of Strings and use the StringTokenizer class to get each token: http://www.java-samples.com/showtutorial.php?tutorialid=236

String val;
ArrayList<String> values = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(str, "+*", true);   // Put the delimiters that you want
while(st.hasMoreTokens()) { 
    val = st.nextToken(); 
    values.add(val);
} 

(I've not tried that example but I think it will work)

davidrgh
  • 853
  • 2
  • 10
  • 17
0

You could use Character.isDigit(a[i]).
But, a character can't be longer than one character, so you'll have to make an array of Strings.

I'd propose:

ArrayList<String> arrList = new ArrayList<String>();
char[] a = str.toCharArray();

for (int i = 0; i < a.length; i++) {
    String s = "";
    s.append(a[i]);
    if(Character.isDigit(a[i+]1)) {
        i++;
        s.append(a[i]);
    }
    arrList.add(s);
}
11684
  • 7,356
  • 12
  • 48
  • 71
0

See EDIT:

package SO;

public class aa {

    public static void main(String[] args) {
    String inputString = "2+32*2-3/3";
    char[] inputCharArray = inputString.toCharArray();
    char[][] finalSplittedArray = new char[100][2];
    int rowCounter = 0;
    int colCounter = 0;
    for (char inputChar : inputCharArray) {
        if (inputChar == '+' || inputChar == '-' || inputChar == '*' || inputChar == '/') {
        rowCounter++;
        finalSplittedArray[rowCounter][0] = inputChar;
        colCounter = 0;
        rowCounter++;
        } else {
        finalSplittedArray[rowCounter][colCounter] = inputChar;
        colCounter++;
        }

    }

    for (char[] cc : finalSplittedArray) {
        for (char c : cc)
        System.out.print(c);
        System.out.println();
    }
    }
}

Basically you need to split string based on operators. Here, I have considered 4 operator : + - * / . Add more if required.

Demo

Priyank Doshi
  • 12,895
  • 18
  • 59
  • 82
0

So, it sounds like you want to tokenize the input into multiple String objects (not into a char array). The Scanner object is intended for exactly this sort of tokenization. Alternatively, you could hand roll an LL(1) parser to do this. At the end of the day, you probably want to have an interface like this:

public interface ExpressionTokenizer {
   Iterable<Token> tokenizeExpression(String expression);
}

I use "Token", here, because you probably do not want to represent "2", "32", and "2" as String, but rather as an "IntegerToken" with an "intValue()" that has already been parsed to an integer.

Here is some psuedo-code to help get you started:

if next_char is a digit:
   tokenize as integer literal
   update state
   return

if next_char is in {'+', '-', '*', '/'}
   tokenize as numeric operator
   update state
   return

tokenize as error
Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
0

How about this?

import java.util.*;
import java.lang.*;
import java.util.StringTokenizer;

class Main {
    public static void main (String[] args) throws java.lang.Exception {
        String str="2+32*2-7/5";
        ArrayList<String> arr = new ArrayList<String>();
        StringTokenizer st = new StringTokenizer(str, "+-*/", true);
        while(st.hasMoreTokens()) { 
                arr.add(st.nextToken());
        }
        System.out.print(arr);
    }
}

Demo

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276