字符串最后一个单词的长度 参与人数:58时间限制:1秒空间限制:32768K 通过比例:6.50% 最佳记录:0 ms|8460K (来自 WrongAnswer) 算法知识视频讲解 题目描述 计算字符串最后一个单词的长度,单词以空格隔开。 输入描述: 一行字符串,长度小于128。 输出描述: 整数N,最后一个单词的长度。 输入例子: hello world 输出例子:
5
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main
{
public Main()
{
// TODO Auto-generated constructor stub
}
public static void main(String[]args) throws FileNotFoundException
{
String line ;
String[] words;
int length;
Scanner scanner = new Scanner( new File("C://Users//Administrator//Desktop//test.txt"));
while(scanner.hasNext())
{
line = scanner.nextLine();
words = line.split(" ");
length= words[words.length-1].length();
System.out.println(length);
}
}
}