我试图创建一个代码,该代码将从用户获取输入并打印出文件路径。我遇到了一个有趣的例子。但是我理解了一条线。构建路径的java代码
//gets input from the user
BufferedReader input = new BufferedReader(newInputStreamReader(s.getInputStream()));
String request = input.readLine();
String path = new String();
int start = 0;
int end = 0;
for (int a = 0; a < request.length(); a++) {
if (request.charAt(a) == ' ' && start != 0) {
end = a;
break;
}
if (request.charAt(a) == ' ' && start == 0) {
start = a;
}
}
path = request.substring(start + 2, end);
为什么在最后添加2?
也许它会截断硬盘驱动器号? - “C:” – stdcall
显然,如果输入中有空格,他们不希望用户输入任何内容,直到第一个空白后的两个字符为止。 –