2017-10-13 6 views
4

文字列と整数値xを指定すると、最初のx文字の末尾に元の文字列の が追加された新しい文字列を返します。 を文字列の最後に移動しようとするには十分な文字数があることを確認してください。If-Else strings

サンプルデータ: データファイル: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; 
     } 
    } 
} 

私の質問は、 ENUUGH文字が印刷されていることを確認するために、インデックス番号が指定された文字列を返します。

答えて

2

String#substringを使用して、文字列の異なる部分を見つけ、+演算子を使用してそれらを連結します。

if (word1.length() < index - 1){ 
    return "no can do"; 
} else { 
    return word1.substring(index) + word1.substring(0, index); 
} 
+0

なぜこの質問には絶対的な基本的な宿題に関する質問にすぎないように見える場合、(現在)3つのアップフォートがありますか? –