【发布时间】:2018-03-25 17:08:45
【问题描述】:
给定一个字符串和一个整数值 x,返回一个包含前 x 个字符的新字符串 原来的字符串现在在最后。确保之前有足够的字符 试图将它们移动到字符串的末尾。
样本数据: 数据文件:stringChopper.dat
apluscompsci 3
apluscompsci 5
apluscompsci 1
apluscompsci 2
apluscompsci 30
apluscompsci 4
样本输出:(输出需要准确)
uscompsciapl
compsciaplus
pluscompscia
luscompsciap
no can do
scompsciaplu
我的代码:
public class StringChopperRunner_Cavazos {
public static void main(String[] args) throws IOException {
Scanner fileIn = new Scanner(new File("stringChopper.dat"));
while(fileIn.hasNext()) {
System.out.println(StringChopper.chopper(fileIn.next(), fileIn.nextInt()));
}
}
}
class StringChopper {
public static String chopper(String word1, int index) {
if(word1.length() < index - 1){
return "no can do";
}
else {
return word1;
}
}
}
所以我的问题是;如果小于该索引号,如何返回带有指示的索引号的字符串以确保打印了足够的字母?
【问题讨论】:
标签: java string if-statement