-1
私はユーザー入力文字列から最初の3文字を取得しようとしていますが、何を試しても、コードは常にIndexOutOfBoundExceptionエラーになります。IndexOutOfBoundエラーなしでJavaの文字列からcharを取得する方法?
コードは次のとおりです。
System.out.print("Enter a year: ");
int year = input.nextInt();
System.out.print("Enter a month: ");
String s = input.nextLine();
char ch0 = s.toUpperCase().charAt(0);
char ch1 = s.toUpperCase().charAt(1);
char ch2 = s.toUpperCase().charAt(2);
if (ch0 == 'J') {
if (ch1 == 'A' && ch2 == 'N') {
System.out.println("Janruary " + year + " has 31 days.");
}
がどのように私はこのエラーを修正しますか?
'String s = input.nextLine(); 'の代わりに' String s = input.next(); 'を使用します。 –
はs.equals(" January ")を使用し、charシンボルは必要ありません –