Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    public static void main(String args[]) {
        try {
            String s1 = "-99,20";
            System.out.println(getNumber(s1));

            s1 = "1,4";
            System.out.println(getNumber(s1));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static int getNumber(String s1) throws Exception {
        Pattern pattern = Pattern.compile("([\\-0-9]*)[,]*.*");
        Matcher m = pattern.matcher(s1);
        if (m.find()) {
            return Integer.parseInt(m.group(1));
        }

        throw new Exception("Not valid input");
    }
}