sigma
sigma

Reputation: 198

Regex lookingAt() in Java

I am trying to write a program MultiplicationTable which displays the multiplication table the user inputs from the command line, so that if the user enters MultiplicationTable 5 7 9, the tables of 5,7 and 9 appear. It also has to cater for input such as MultiplicationTable 7-11, which would display the tables from 7 to 11. The following is what I wrote:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MultiplicationTable{

    static void multTable(int n){
        for(int i=0; i<=12; i++){
            System.out.printf("%2s", i);
            System.out.println(" * " + n + " = " + i*n);
        }
    }   

    static String REGEX = "-";

    public static void main (String[] args){

        Pattern p = Pattern.compile(REGEX);

        if(args.length == 0)
            System.out.println("Error! No multiplication table entered!");
        else{
            for(int i=0; i<args.length; i++){
                Matcher m = p.matcher(args[i]); 
                if(m.lookingAt()){  // If a dash is found
                    int start = Integer.parseInt(args[i].substring(0, m.start()-1));
                    int end = Integer.parseInt(args[i].substring(m.start()+1, args[i].length()-1));
                    System.out.println(start + "," + end);
                    /*for(int j=start; j<=end; j++)
                        multTable(j);
                }
                else{
                    multTable(args[i].Integer.parseInt(substr(0, args[i].length-1)));*/
                }
            }
        }
    }
}

The problem is that the program is not going into this if statement:

if(m.lookingAt()){

The System.out.println(start + "," + end); was added as a test and when the command MultiplicationTable 7-11 was executed, the values are not being displayed. Am I not getting something about the way lookingAt() works, or am I using it incorrectly?

Upvotes: 0

Views: 827

Answers (1)

Ravikumar
Ravikumar

Reputation: 901

lookingAt() will not work in your case as the method only matches the regular expression against the beginning of the text.

In simple words, the String should start with the regex pattern.

Illustration with code -

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String str1 = "7-11";
        String str2 = "-11";

        String regex = "-";
        Pattern p = Pattern.compile(regex);
        Matcher m1 = p.matcher(str1);
        Matcher m2 = p.matcher(str2);
        System.out.println("Does str1 matches with regex using lookingAt method " + m1.lookingAt());
        System.out.println("Does str2 matches with regex using lookingAt method " + m2.lookingAt());
    }
}

Output -

Does str1 matches with regex using lookingAt method false
Does str2 matches with regex using lookingAt method true

So for your problem you can use find() method which searches for occurrences of the regular expressions in the text passed to the matcher, If multiple matches can be found in the text, the find() method will find the first, and then for each subsequent call to find() it will move to the next match.

You can change your if condition to if(m.find()). Click here to see working code.

If its okay to use any other way apart from regex, then you can split the String by - which will return an String array.

For example

public static void main (String[] args){
    String str = "7-11";
    String[] stringArray = str.split("-");

    System.out.println(stringArray[0]); // 7
    System.out.println(stringArray[1]); // 11
}

Upvotes: 1

Related Questions