今天在比比宝复试,遇到了如下题,希望大家互相讨论一下 1有一段字符串,写程序统计其中的单词数,例如:This is tom 2编写程序,判断一个给定的字符串是否为有效的IP地址,输出判断结果。IP地址每个数字必须在0到255之间,例如: "10.1.1.1"应输出"10.1.1.1 is a valid IP address","10.1.1.299"应输出"10.1.1.299 is a invalid IP address" 3编写一个完整的程序,完成从一个指定的完整的文件路径(如:"C:\My Documents\..")中析取文件名,扩展名和文件所处目录的功能。

解决方案 »

  1.   

    Q1:
    public class Statistics
    {
    public static void main(String[] args) 
    {
    String str = "This is tom";
    String[] a = str.split( " " );
    System.out.println( a.length );
    }
    }
    Q2:
    class IP
    {
    public static String isValid( String ip )
    {
    String[] a = ip.split( "." );
    for ( int i = 0; i < a.length; i++ )
    {
    if ( Integer.parseInt( a[i] ) > 255 )
    {
    return ip + " is a valid IP address.";
    }
    }
    return ip + " is a invalid IP address.";
    }
    public static void main(String[] args) 
    {
    System.out.println( isValid( "10.1.1.1" ) );
    System.out.println( isValid( "10.1.1.299" ) );
    }
    }Q3:弱弱的问下,比比宝是什么公司?
      

  2.   

    Q3public class testdir{
      public static void main(String[] args)
      {
        String directory="C:\\My Documents\\test.txt";
     
        String  extendname=null; //扩展名
        String  path=null;  //路径
        String  filename=null;
      
        String[] temp=directory.split("\\."); //注意这个
        
             path=temp[0].substring(0,temp[0].lastIndexOf("\\"));
        filename=temp[0].substring(temp[0].lastIndexOf("\\")+1);
        extendname="."+temp[temp.length-1];
        System.out.println("路径:"+path);
        System.out.println("文件名:"+filename);
        System.out.println("后缀:"+extendname);
       }
    }
    Q2:class IP
    {
        public static String isValid( String ip )
        {
            String[] a = ip.split( "\\." );//修改了这里
            for ( int i = 0; i < a.length; i++ )
            {
                if ( Integer.parseInt( a[i] ) >255 )
                {
                    return ip + " is a Invalid IP address.";//this
                }
            }
            return ip + " is a valid IP address.";//this
        }
        public static void main(String[] args) 
        {
            System.out.println( isValid( "10.1.1.1" ) );
            System.out.println( isValid( "10.1.1.299" ) );
        }
    }
      

  3.   


    import java.util.StringTokenizer;
    import static java.lang.System.out;
    public class CountWords
    {
    public static void main(String[] args)
    {
    StringTokenizer st = new StringTokenizer("This is tom");
    out.println("length:"+st.countTokens());
    }
    }import java.util.StringTokenizer;
    import static java.lang.System.out;public class IPAddress
    {
    public static void main(String[] args)
    {
    String IP="153.0.999.1";

    out.println(isIP(IP));

    }
    public static boolean isIP(String str)
    {
    String[] strs = str.split("\\.");
    if(strs.length != 4)
    {
    return false;
    }

    for(String s:strs)
    {
    try{
    short num = Short.parseShort(s);
    if(num < 0 || num > 255)
    {
    return false;
    }
    }catch(NumberFormatException e)
    {
    return false;
    }
    }
    return true;
    }
    }import static java.lang.System.out;
    public class FilePath
    {
    public static void main(String[] args)
    {
    String filePath = "D:\\reference\\java\\mysql.txt";
    int dotIndex = filePath.lastIndexOf(".");
    out.println("extention:"+filePath.substring(dotIndex+1));
    int backslashIndex = filePath.lastIndexOf("\\");
    out.println("filename:"+filePath.substring(backslashIndex+1,dotIndex));
    out.println("file dir:"+filePath.substring(0,backslashIndex));
    }
    }
      

  4.   

    感觉 像在考 PHP的 函数运用0 0
      

  5.   


    import java.util.regex.Pattern;public class StartMothod { private static final String SUCCESSRESULT = "this is a valide ip address.";
    private static final String FAILURERESULT = "this is a invalide ip address.";

    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String strCheckIp = checkIp();
    System.out.println(strCheckIp);
    } /**
     * check ip method
     * @return
     */
    private static String checkIp() {
    String ipFirst = "123.2.0.123";
    String[] strIp = new String[20];
    strIp = ipFirst.split("\\.");
    if (strIp.length != 4) {
    return FAILURERESULT;
    }
    for (int i = 0; i < strIp.length; i++) {
    if(!isNumeric(strIp[i])){
    return FAILURERESULT;
    }
    if (Integer.parseInt(strIp[i]) > 255
    || Integer.parseInt(strIp[i]) < 0) {
    return FAILURERESULT;
    }
    }
    return SUCCESSRESULT;
    }

    /**
     * check number method
     * @param str
     * @return
     */
    private static boolean isNumeric(String str){
    Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }}