useDelimiter("//D")

I got this line "(123+(45 -3))*4000/-2" read in by a scanner

Scanner scan1= new Scanner(line); //scan by tokens

Scanner scan2 = new Scanner(line).useDelimiter("\\D"); //scan by numbers

The problem is that scan2 takes in the numbers and also white spaces if there are 2 or more arithmetic operators +,-,/,*, or parenthesis ( and )

How do i get rid of the white spaces being read in?

[412 byte] By [javaPOTATOa] at [2007-11-26 20:21:29]
# 1
You want next() to always return the numbers?Scanner scan2 = new Scanner(line).useDelimiter("\\D+"); Every occurence of one or more non-digit characters will be a delimiter.
jverda at 2007-7-10 0:46:15 > top of Java-index,Java Essentials,Java Programming...
# 2
Yes. next should only return numbers.\\D+ works but it does not take in the "/-" at the last part.Hence the last two values of my arraylist only stores 4000, 2 when it should have 4000, /, -, 2, Any idea why it skips only that?
javaPOTATOa at 2007-7-10 0:46:15 > top of Java-index,Java Essentials,Java Programming...
# 3
Why don't you use a StringTokenizer?
qUesT_foR_knOwLeDgea at 2007-7-10 0:46:16 > top of Java-index,Java Essentials,Java Programming...
# 4
Short answer: Regex is not well suited to this kind of task.
jverda at 2007-7-10 0:46:16 > top of Java-index,Java Essentials,Java Programming...
# 5

Presumably you are trying to write some sort of a expression parser. If so, you are not on the right track.

Read this thread: http://forum.java.sun.com/thread.jspa?threadID=784915

and especially marlin314's excellent contributions to get a notion of the things you need to do.

Good luck.

prometheuzza at 2007-7-10 0:46:16 > top of Java-index,Java Essentials,Java Programming...
# 6

marlin314 explanation is indeed detailed!

But what I really need to do is rather simple. I'm required to pass an expression through a scanner and tokenise each operator and operand to be stored in an arraylist.

Unfortunately for me, the tokeniser method cannot be used because its considered a legacy class.

So far everything seems to be going just fine except passing the value 4000 will cause my method to not take in any operators after that. =(

javaPOTATOa at 2007-7-10 0:46:16 > top of Java-index,Java Essentials,Java Programming...
# 7

> marlin314 explanation is indeed detailed!

> But what I really need to do is rather simple. I'm

> required to pass an expression through a scanner and

> tokenise each operator and operand to be stored in an

> arraylist.

> Unfortunately for me, the tokeniser method cannot be

> used because its considered a legacy class.

>

> So far everything seems to be going just fine except

> passing the value 4000 will cause my method to not

> take in any operators after that. =(

Something like this?import java.util.regex.*;

// ...

String expression = "(123 +(45 -3))*4000/-2";

String regex = "[0-9]+|\\+|\\*|/|-";

Pattern pattern = Pattern.compile(regex);

Matcher matcher = pattern.matcher(expression);

while(matcher.find()) {

System.out.println(matcher.group());

}

produces the following output:/*

123

+

45

-

3

*

4000

/

-

2

*/

prometheuzza at 2007-7-10 0:46:16 > top of Java-index,Java Essentials,Java Programming...
# 8

Almost! But I need to add them into an arraylist so each address in the arraylist will be the running order of the expression.

This is what i have now.

Scanner token = new Scanner(inLine);

while(token.hasNext()){

try{

String line = token.next();

Scanner operands = new Scanner(line).useDelimiter("\\D+");

for(int i=0; i<line.length(); i++){

char sent = line.charAt(i);

switch(sent){

case '+':

tokenLine.add("+");

break;

case '-':

tokenLine.add("-");

break;

case '/':

tokenLine.add("/");

break;

case '*':

tokenLine.add("*");

break;

case '(':

tokenLine.add("(");

break;

case ')':

tokenLine.add(")");

break;

case '=':

tokenLine.add("=");

break;

default:

String num = operands.next();

tokenLine.add(num);

break;

}

}

}catch(Exception e){}

}'>

javaPOTATOa at 2007-7-10 0:46:16 > top of Java-index,Java Essentials,Java Programming...
# 9

> Almost! ...

Ah, I see you want to keep the parentheses. Try this:import java.util.*;

import java.util.regex.*;

public class Main {

public static void main(String[] args) {

String expression = "(123 +(45 -3))*4000/-2";

String regex = "[0-9]+|\\+|\\*|/|-|\\(|\\)";

Pattern pattern = Pattern.compile(regex);

Matcher matcher = pattern.matcher(expression);

List<String> tokens = new ArrayList<String>();

while(matcher.find()) {

tokens.add(matcher.group());

}

System.out.println(tokens);

}

}

Output:/*

[(, 123, +, (, 45, -, 3, ), ), *, 4000, /, -, 2]

*/

prometheuzza at 2007-7-10 0:46:16 > top of Java-index,Java Essentials,Java Programming...
# 10
I swear if you're living in my state i'll send you a thank you card. =)Thanks prometheuzz !!1 more little thing for me please. What does this line means? -> String regex = "[0-9]+|\\+|\\*|/|-|\\(|\\)"; I like to be taught how to fish... =)
javaPOTATOa at 2007-7-10 0:46:16 > top of Java-index,Java Essentials,Java Programming...
# 11

> I swear if you're living in my state i'll send you a

> thank you card. =)

; )

> Thanks prometheuzz !!

You're welcome.

> 1 more little thing for me please.

>

> What does this line means? -> String regex = "[0-9]+|\\+|\\*|/|-|\\(|\\)";

It's pretty straight forward actually. The following "groups" get extracted from your expression String:[0-9]+one or more numbers

| OR

\\+the + char

| OR

\\*the * char

| OR

/ the / char

| OR

- the - char

| OR

\\(the ( char

| OR

\\)the ) char

the charcters +, /, ( and ) have a special meaning in the regex-language, so they need to be escaped with \\

> I like to be taught how to fish... =)

Good to hear that.

prometheuzza at 2007-7-10 0:46:16 > top of Java-index,Java Essentials,Java Programming...
# 12

You can split up the regex for more clarity and even expand it with the introduction of decimal numbers like this:import java.util.*;

import java.util.regex.*;

public class Main {

public static void main(String[] args) {

String expression = "(123.3 +(45 -3))*4000/-2";

String integer = "[0-9]+";

String decimal = integer+"\\."+integer; // '.' is also a special regex-character!

String operator = "\\*|\\+|-|/";

String parenthesis = "\\(|\\)";

// 'decimal' needs to come before 'integer'

String regex = decimal+"|"+integer+"|"+operator+"|"+parenthesis;

Pattern pattern = Pattern.compile(regex);

Matcher matcher = pattern.matcher(expression);

List<String> tokens = new ArrayList<String>();

while(matcher.find()) {

tokens.add(matcher.group());

}

System.out.println(tokens);

}

}

Checkout this link:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

prometheuzza at 2007-7-10 0:46:16 > top of Java-index,Java Essentials,Java Programming...
# 13
> ...> the charcters +, /, ( and> ) have a special meaning in the> regex-language, so they need to be escaped with> \\> ...I meant +, *, ( and ) are special.
prometheuzza at 2007-7-10 0:46:16 > top of Java-index,Java Essentials,Java Programming...
# 14
Fantastic! Many thanks again! =)
javaPOTATOa at 2007-7-10 0:46:16 > top of Java-index,Java Essentials,Java Programming...
# 15
> Fantastic! Many thanks again! =)You're welcome again.; )
prometheuzza at 2007-7-21 17:55:29 > top of Java-index,Java Essentials,Java Programming...