Java - Write code to convert String to Int and handle null value

Requirements

Write code to convert String to Int

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String s = "book2s.com";
        System.out.println(toInt(s));
    }/*from  ww  w  . j  av  a  2  s. c o  m*/

    public static int toInt(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        return Integer.parseInt(s);
    }
}

Related Exercise