Given a string and an integer value x, return a new string with the first x characters of the original string now at the end. Make sure there are enough characters before attempting to move them to the end of the string.
Sample Data : Data File: stringChopper.dat
apluscompsci 3
apluscompsci 5
apluscompsci 1
apluscompsci 2
apluscompsci 30
apluscompsci 4
Sample Output : (output needs to be exact)
uscompsciapl
compsciaplus
pluscompscia
luscompsciap
no can do
scompsciaplu
My code:
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;
}
}
}
So my question is; How can I return the String with the index number indicated if it's less than that to make sure there are ENOUGH letters printed?