Solved

Extension of "Detecting a Character" question

Posted on 2004-10-12
13
217 Views
Last Modified: 2010-05-18

Basically the question is answered but i need more...

What i need is to hav a statement that detects if the string has items other than:

any integer, (, ), +, -, / and *.

if any of the above r detected thats ok... anything else is bad

so basically,

if ( not any integer, (, ), +, -, / and *) {
    System.err.println("INVALID");
    System.exit(1);
}
0
Comment
Question by:AnthonyCosenza
  • 6
  • 4
  • 3
13 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12288403
String str="Abcd";

if(str.matches("A-Za-z"))
{
//ok
}
else
{
//not ok
}
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12288445
oops sorry wrong answer
0
 

Author Comment

by:AnthonyCosenza
ID: 12288455
ok, i'm not just worried about characters... i'm worried about things like "#" or "\"

basically any small little piece of text that could cause mischief

Thanks
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12288569
booleab b=str.matches.(", \\(\\ ) \\+\\ - / \\ *");
if(b)
for(int i=0;i<str.length);i++)
{
  if(!Character.isDigit(str.charAt(i)
  {
      b=false;
        break;  
  }

}

if(b)
//ok
else
//not ok
0
 

Author Comment

by:AnthonyCosenza
ID: 12288630
May i ask wat this line is doin??

if(!Character.isDigit(str.charAt(i)

Thanks again
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12289218
ignore previous one
try this

    if(str.matches("[0-9\\,\\(\\)\\+\\-\\*]+"))
    {
      System.err.println("ok");
    }
    else
    {
              System.err.println("not ok");
    }
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 250 total points
ID: 12289237
missed / character

This works

if(str.matches("[0-9\\,\\(\\)\\+\\-\\*/]+"))
    {
      System.err.println("ok");
    }
    else
    {
              System.err.println("not ok");
    }
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12290944
>>May i ask wat this line is doin??
>>if(!Character.isDigit(str.charAt(i)

It checks if the character at position i in the String str is a digit
0
 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
ID: 12291256
Anthony,

I think you want to allow
1) all digits
2) (
3) )
4) +
5) -
6) /
7) *

No commas, right?

Then this is the reg expression:

     if ( !str.matches("[0-9\\(\\)\\+\\-\\*/]+") ) {
        System.err.println("INVALID");
        System.exit(1);
    }

If you want to allow spaces too:  (because now "5 + 6" is invalid)

     if ( !str.matches("[0-9 \\(\\)\\+\\-\\*/]+") ) {
        System.err.println("INVALID");
        System.exit(1);
    }

Remarks:
[1]
This doesn't recognize weird strings like
        String str = "6+*/-5-15*-6"
as invalid.

[2]
All six comments you accepted already are graded with a B.
Is that the maximum grade you'll ever give?
Here at EE, were used to get an A for correct answers/solutions,
a B for answers that helped to find the correct answer/solution and
a C for answers that help a little bit in finding the solution.

Did you know that giving an A-grade doesn't cost *you* any more points than the # originally set, while *we* get more points?
(A=x4, B=x3 and C=x2)
0
 

Author Comment

by:AnthonyCosenza
ID: 12292807
I've only been givin B grades because they are helpful solutions yet they r only parts of a bigger problem...
I had no idea that they affected the points system.

Anyway.. i hav another problem..

I hav something like this

while (st.hasMoreTokens( )) {
    if ( !token.matches("[0-9\\(\\)\\+\\-\\*/]+") ) {
        System.err.println("INVALID EXPRESSION");
        System.exit(1);
    } else {
        System.out.println(token);
        token = st.nextToken( );
    }
}
System.exit(1);

Where i hav a StringTokenizer goin through the string which is in a file...
What happens here is if i hav a expression in the file, for example ( 4 + 3 )..
it prints:

(
4
+
3

.. It doesnt print watever the last item is...

Puzzled..
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12295181
Can you show us what StringTokenizer you use?

Remark:
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

Thanks for accepting
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12295393
Thanks for points :)

zzynx,
The last question was already answered here http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21165896.html by CEHJ  

;-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12295406
>> The last question was already answered
Always handy to know. ;°)
Thanks sudhakar.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 60
expectj telnet failing 5 45
Configure a Bean in an XML file 4 42
Bot application - advice 3 37
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question