Given a string, which is the equation (23 + 95 * 54, for example). It is necessary to divide it into two arrays, one of which contains numbers and the second one. How to do it?

  • one
    Pass the entire character by character if the character is a digit in one array. In other cases - in the other. What is the problem? - Vladimir Martyanov
  • Just want to know how to do it with minimal time and memory - Hitrene
  • 2
    The variant offered by me will be fast enough and will not require extra memory. If you need faster - write on it. - Vladimir Martyanov
  • Regular expressions work faster on long lines than a character-by-character traversal. - Pavel Krizhanovskiy

3 answers 3

Pass the entire character by character if the character is a digit in one array. In other cases - in the other. This option will be fast enough and will not require extra memory. If you need faster - write on it.

  • Thank. And how to add numbers that contain more than two characters? I decided it this way: for (char i : data) { if (i == '+' || i == '*' || i == '-') { symbols.add(i); numbers.add(oper); oper = 0; } else { oper=oper * 10 + Character.getNumericValue(i); } } for (char i : data) { if (i == '+' || i == '*' || i == '-') { symbols.add(i); numbers.add(oper); oper = 0; } else { oper=oper * 10 + Character.getNumericValue(i); } } for (char i : data) { if (i == '+' || i == '*' || i == '-') { symbols.add(i); numbers.add(oper); oper = 0; } else { oper=oper * 10 + Character.getNumericValue(i); } } That is, a trivial multiplication of the previous number by 10 and adding a new one to it. I thought about options with substring. What's better? - Hitrene
  • @Hitrene no problem, you have not fully formulated the task. - Vladimir Martyanov

Here, sketched the solution on regular expressions:

 import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while(true) { String input = br.readLine(); Pattern pNum = Pattern.compile("\\b[0-9]\\.+\\b"); Pattern pAct = Pattern.compile("[+\\-*/]"); List<Integer> nums = new ArrayList<>(); List<String> acts = new ArrayList<>(); Matcher mNum = pNum.matcher(input); Matcher mAct = pAct.matcher(input); while (mNum.find()) System.out.println(mNum.group()); //вместо вывода добавляйте в список nums для дальнейшей работы с ними while (mAct.find()) System.out.println(mAct.group()); //вместо вывода добавляйте в список acts для дальнейшей работы } } } 

Sample I / O:

23 + 95 * 54

 23 95 54 + * 

23 + 95 * 54 - 10050.21

 23 95 54 10050.21 + * - 

And then to calculate the value of the expression, take one by one from nums and acts and count.

     List<Character> listName = new ArrayList<Character>(); List<Double> numbers = new ArrayList<Double>(); StringTokenizer st1 = new StringTokenizer("+-/*"); StringTokenizer st2 = new StringTokenizer("0123456789"); while(st1.hasMoreTokens()){ numbers.add(Double.parseDouble(st.nextToken())); } while(st2.hasMoreTokens()){ listName.add(st.nextToken()); //вот тут возможно проблема, можно пошаманить и привести к уму } 
    • Try to write more detailed answers. I am sure the author of the question would be very grateful for the comments on the decision. - Nicolas Chabanovsky