From 9e8f9e2a9550d771e1c38dec3f2bd369ceb2a5d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=88=B4=E6=9C=A8=E3=80=80=E7=9A=93=E4=BB=8B?= <26-k-suzuki-avnz@it-college.co.jp> Date: Fri, 9 Oct 2026 15:32:04 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=9F=E3=83=83=E3=82=B7=E3=83=A7=E3=83=B3?= =?UTF-8?q?=E9=96=8B=E7=99=BA=EF=BC=93=E6=97=A5=E7=9B=AE=EF=BC=9A=E6=AE=B5?= =?UTF-8?q?=E9=9A=8E6=E3=81=BE=E3=81=A7=E5=AE=8C=E4=BA=86=E6=B8=88?= =?UTF-8?q?=E3=81=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- StringCheck_suzuki_1009.java | 137 +++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 StringCheck_suzuki_1009.java diff --git a/StringCheck_suzuki_1009.java b/StringCheck_suzuki_1009.java new file mode 100644 index 0000000..5f3da80 --- /dev/null +++ b/StringCheck_suzuki_1009.java @@ -0,0 +1,137 @@ +import java.util.NoSuchElementException; +import java.util.Scanner; + +public class StringCheck { + + /** + * @param args + */ + public static void main(String[] args) { + // TODO 自動生成されたメソッド・スタブ + + // 表示文字列を設定する + String[] display; + display = new String[4]; + display[0] = "いまなにしてる?"; + display[1] = "何も入力されていません"; + display[2] = "投稿文が長すぎます"; + display[3] = "↓↓↓↓↓↓ 投稿されました ↓↓↓↓↓↓"; + + /*==========変数定義==========*/ + + String str; // 入力文章 +// int lim = 10; // 文字数上限 + + /*=========動作==========*/ + + while (true) { // 空文字or文字数超過→再度入力待機状態に戻る + + /*==========文字列読み取り==========*/ + + // いまなにしてる? + System.out.println(display[0]); + + // 標準入力から1行読み込む+前後の空白を除去する + str = inputString(); + + /*==========文字数判定==========*/ + + // 空文字判定 + if ( isEmptyInput(str) ) { + System.out.println("何も入力されていません"); + continue; + } + // 文字数超過判定 + if ( isTooLongInput(str) ) { +// if ( isTooLongInput(str,lim) ) { + System.out.println("投稿文が長すぎます"); + continue; + } + + /*==========正常入力==========*/ + + // 空行を表示する + System.out.println(); + + // ↓↓↓↓↓↓ 投稿されました ↓↓↓↓↓↓ + System.out.println(display[3]); + + // 文字列を出力する + System.out.println(str); + +// Submitted(str, display); + + break; + + } + + } + + + // 標準入力から1行読み込む+前後の空白を除去する + public static String inputString() { + + String str = ""; + + try { + + // 標準入力から1行分の文字列を読み取りstrに格納する + Scanner sc = new Scanner(System.in); + str = sc.nextLine(); + //sc.close(); // NoSuchElementException + + // 文字列の前後の空白を除去する + str = str.strip(); + + return str; + + } catch ( NoSuchElementException err ) { + + System.out.println("入力が受け付けられなくなったため、プログラムを終了します。"); + System.exit(0); // 終了 + + } catch ( IllegalStateException err ) { + + System.out.println("入力が受け付けられなくなったため、プログラムを終了します。"); + System.exit(0); // 終了 + + } + + return str; + + } + + // 空文字判定 + public static boolean isEmptyInput( String str ) { + + return str.length() == 0 ; + + } + + // 文字数超過判定 + public static boolean isTooLongInput( String str ) { + + return str.length() > 140 ; + + } + + // 文字数超過判定(上限指定) + public static boolean isTooLongInput( String str , int lim ) { + + return str.length() > lim ; + + } + + // 投稿完了時 +// public static void Submitted( String str , String[] display ) { +// +// // 空行を表示する +// System.out.println(); +// // ↓↓↓↓↓↓ 投稿されました ↓↓↓↓↓↓ +// System.out.println(display[3]); +// // 文字列を出力する +// System.out.println(str); +// +// } + +}