方法1:利用substring和StringBuilder累计

import java.util.*;
public class Main{
    public static void main(String [] args){
        getStringLength();
    }

    public static void getStringLength(){
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine();
        int len = input.length();
        if(0 == len){
            System.out.print(0);
        }
        StringBuilder ret = new StringBuilder();
        String temp = "";

        for(int i = len-1; i >= 0; i--){
            temp = input.substring(i, i+1);
            if(" ".equals(temp)){
                break;
            }
            ret.append(temp);
        }       
        System.out.print(ret.length());
    }

}

方法2:split方法切割为数组

import java.util.*;
public class Main{
    public static void main(String [] args){
        getStringLength();
    }

    public static void getStringLength(){
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine();
        int len = input.length();
        if(0 == len){
            System.out.print(0);
        }
        String [] str = input.split(" ");
        String temp = str[str.length - 1];
        System.out.print(temp.length());
    }

}