#isInteger method with regex

52 messages · Page 1 of 1 (latest)

honest echo
#

So basically this is my method but when for isIntger but when I call I try it, it compiles but nothing gets printed out, why?

 private static boolean isInteger(String s) {
        if(s.matches("[0-9]")){
        return true;}
        else{
            return false;
        }
    }

    public static void main(String[] args) {
        isInteger("2");
    }
pine berryBOT
#

This post has been reserved for your question.

Hey @honest echo! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

lucid badger
#

Why would it print?

honest echo
lucid badger
#

You are infact returning true or false

honest echo
#

but one more question tho

lucid badger
#

whasts up

honest echo
#

How do I check using regex if something is an operator

lucid badger
#

What type of operator?

honest echo
#

+-* /

#

Also I forgot that [0-9] only works for 0-10, and it returns false whenever I have a higher value than 9

lucid badger
#

Its just in the string itself, instead of [0-9] you can do ^[0-9]+$

#

Where the ^ is the start of the input, and $ is the end of the inpput. + here is for multiple occurrences

honest echo
#

nevermind

#

btw how do I check if it contains - in the beginings to include negative numbers

lucid badger
#

What is your overall goal here?

honest echo
#

To check if the value that is passed in is an integer

lucid badger
#

^-?[0-9]+$

#

Is the input of this value a string? Why not use integers for this

honest echo
#

also I didn't make the method header

#

btw wouldn't ^-?[0-9]+$ check if there are - in the middle also

#

I mean wouldn't my method return true if s= 23-434

lucid badger
#

no

honest echo
#

what does ^ do then?

#

isn't it like a starter that say beginning from this

lucid badger
#

Matches the beginning of the line

honest echo
lucid badger
#

Couple of things. Firstly I would recommend reading up and learning more about regex, secondly create a unit test and test against that.

honest echo
lucid badger
#

What the the exact problem saying? I'm not going spoon feed answers, but I will help you learn

honest echo
#

I wanna learn

lucid badger
#

So in lament terms, it matches the start of the string. In that regex you are basically saying this by using the ^.
"My string has to match starting with 0-9. If it does then im saying its true. If it does not, I am saying it is false"

#

Example:
^A matches any input string that would start with "A"

#

In your instance, ^[0-9] is measuring any input string that would start with 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9.

#

When you add in -? what you are doing is adding two expressions, the first being the - So in theory, if you had ^-[0-9] you would only match an input that started with -. For instance, -10 would be true, but 10 would be false still. So to change this, we add ? after the - to indicate that it is optional. We give it the option to say it can start with or without a -

#

Does this help?

honest echo
lucid badger
#

lmk if you have further questions

honest echo
#
public static int evaluate(String expr) throws ExpressionException {
        string splitByNum = "[0-9]";
        string splitByOp = "-";
        string [] numberOfCharacters = expr.split(splitByNum, "-", "+", "*", "/");
        

        return 0
    }```
#

Did I do right or?

#

or by using regex

lucid badger
#

What is the outcome you expect to happen? Give an example

honest echo
#

The plan is to devide the Postfix expression I have and then build a stack using a linkedlist class I built

#

so what I wanna do is if expr = "32*3+" or something like this I wanna separate all of them and put them in an array and find the length and then after that I can finally start with my stack