Write code to convert String To Int
If fail, return a default value
Use Integer.parseInt method
//package com.book2s; public class Main { public static void main(String[] argv) { String str = "book2s.com"; System.out.println(convertStringToInt(str)); }//from w w w. j a v a 2s. c o m public static int convertStringToInt(String str) { return convertStringToInt(str, 0); } public static int convertStringToInt(String str, int failValue) { if (str == null) { return failValue; } try { return Integer.parseInt(str); } catch (Exception e) { e.printStackTrace(); } return failValue; } }