Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 04e544ae85 | |||
| 709e454380 | |||
| a85256b39d | |||
| 107e11a567 | |||
| d3086351dd | |||
| 040e0d8b48 | |||
| dc289e7821 | |||
| a8ba4e4098 | |||
| 0efa10e7df | |||
| 22920ecacb | |||
| c265cee178 | |||
| 061c8c36dc | |||
| aa08ab7f8e |
60
StringCheck/src/StringCheck.java
Normal file
60
StringCheck/src/StringCheck.java
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class StringCheck {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// 初期化・宣言
|
||||||
|
String[] classNames = {
|
||||||
|
"いまなにしてる?",
|
||||||
|
"何も入力されていません",
|
||||||
|
"投稿文が長すぎます",
|
||||||
|
"↓↓↓↓↓↓ 投稿されました ↓↓↓↓↓↓",
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
//標準入力から1行分の文字列を読み込む
|
||||||
|
Scanner scan = new Scanner(System.in);
|
||||||
|
|
||||||
|
String str = "";
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
System.out.println(classNames[0]);
|
||||||
|
str = scan.nextLine();
|
||||||
|
|
||||||
|
// 前後の空白を除去した文字列
|
||||||
|
String trimmed = str.strip();
|
||||||
|
|
||||||
|
// 文字列の長さを取得
|
||||||
|
int length = trimmed.length();
|
||||||
|
|
||||||
|
// 空字判定(空白だけも含む)
|
||||||
|
boolean isEmpty = (length == 0);
|
||||||
|
|
||||||
|
// 文字数超過判定
|
||||||
|
boolean isOverLimit = (length > 140);
|
||||||
|
|
||||||
|
// 残り入力可能文字数
|
||||||
|
int a = (140 - length);
|
||||||
|
|
||||||
|
// if文による検証ロジック
|
||||||
|
if (isEmpty) {
|
||||||
|
System.out.println(classNames[1]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (isOverLimit) {
|
||||||
|
System.out.println(classNames[2]);
|
||||||
|
continue;
|
||||||
|
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
System.out.println(classNames[3]);
|
||||||
|
//読み込んだ文字列の前後にある空白文字を除去
|
||||||
|
System.out.println(str.strip());
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
113
StringCheck/src/day3/StringCheck.java
Normal file
113
StringCheck/src/day3/StringCheck.java
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
package day3;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* StringCheck.java
|
||||||
|
* ------------------------------------------------------------
|
||||||
|
* 【1日目・2日目終了時点のプログラム】
|
||||||
|
*
|
||||||
|
* これは、3日目ミッション「段階1〜3(メソッドへの切り出し)」の
|
||||||
|
* 出発点として使う、リファクタリング前のベースコードです。
|
||||||
|
*
|
||||||
|
* 3日目の段階1〜3では、このファイルの「動作(画面の表示内容)」は
|
||||||
|
* 一切変えずに、コードの「構造」だけを inputString() / setUpLogger()
|
||||||
|
* という別メソッドに整理し直していきます。
|
||||||
|
* これが実務でいう「リファクタリング(外から見た動きを変えずに、
|
||||||
|
* 中身の構造を整理すること)」です。
|
||||||
|
*
|
||||||
|
* この時点では、まだ次のことを行っていません(後日のミッションで扱います)。
|
||||||
|
* ・main以外のメソッドへの切り出し
|
||||||
|
* ・ログ出力(Logger)の実装
|
||||||
|
* ・例外処理(try-catch)
|
||||||
|
* ------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
public class StringCheck {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// 1日目 段階1:画面に表示する文言を、あらかじめ配列にまとめておく
|
||||||
|
// ============================================================
|
||||||
|
// 何度も使う固定の文言は、1つずつバラバラに書くのではなく、
|
||||||
|
// 配列にまとめておくと管理しやすい。
|
||||||
|
// messages[0] … 入力を促すプロンプト
|
||||||
|
// messages[1] … 未入力(または空白のみ)のときのエラーメッセージ
|
||||||
|
// messages[2] … 文字数が多すぎるときのエラーメッセージ
|
||||||
|
// messages[3] … 投稿が成功したときに表示するバナー
|
||||||
|
String[] messages = {
|
||||||
|
"いまなにしてる?",
|
||||||
|
"何も入力されていません",
|
||||||
|
"投稿文が長すぎます",
|
||||||
|
"↓↓↓↓↓↓ 投稿されました ↓↓↓↓↓↓"
|
||||||
|
};
|
||||||
|
|
||||||
|
// 投稿内容(前後の空白を除去したあとの文字列)を保持する変数。
|
||||||
|
// while文を抜けたあとでも使うので、while文の外で宣言しておく。
|
||||||
|
String str = "";
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// 2日目 段階1:while(true) で「正しい入力が得られるまで繰り返す」
|
||||||
|
// ============================================================
|
||||||
|
// 何回繰り返すかがあらかじめ決まっていないので、while(true) の
|
||||||
|
// 無限ループにして、条件を満たしたときに break でループを抜ける
|
||||||
|
// 作りにする。
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
// ---- 1日目 段階3:プロンプトの表示 ----
|
||||||
|
// 配列の0番目に入れておいたプロンプト文言を表示する。
|
||||||
|
System.out.println(messages[0]);
|
||||||
|
|
||||||
|
str = inputString();
|
||||||
|
// ============================================================
|
||||||
|
// 2日目 段階2:if文による検証ロジック
|
||||||
|
// ============================================================
|
||||||
|
if (isEmptyInput(str)) {
|
||||||
|
// 何も入力されていない(または空白のみ入力された)場合
|
||||||
|
System.out.println(messages[1]);
|
||||||
|
// continue で、while文の先頭(プロンプト表示)に戻り、
|
||||||
|
// もう一度入力をやり直してもらう。
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isTooLongInput(str)) {
|
||||||
|
// 141文字以上入力された場合
|
||||||
|
System.out.println(messages[2]);
|
||||||
|
// continue で、while文の先頭に戻る。
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ここまで到達したということは、
|
||||||
|
// 「空文字でもなく、文字数超過でもない」=正しい入力が得られた
|
||||||
|
// ということなので、break でループを抜ける。
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// 1日目 段階3:投稿完了処理(検証をクリアしたあとの表示)
|
||||||
|
// ============================================================
|
||||||
|
// 空行を1行はさんでから、投稿完了バナーと投稿内容を表示する。
|
||||||
|
System.out.println();
|
||||||
|
System.out.println(messages[3]);
|
||||||
|
System.out.println(str);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String inputString() {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
return scanner.nextLine().strip();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isEmptyInput(String str) {
|
||||||
|
boolean isEmpty = (str.length() == 0);
|
||||||
|
return isEmpty;
|
||||||
|
|
||||||
|
}
|
||||||
|
public static boolean isTooLongInput(String str) {
|
||||||
|
boolean isTooLong = (str.length() > 140);
|
||||||
|
return isTooLong;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 見直しできました
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<classpath>
|
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
|
|
||||||
<attributes>
|
|
||||||
<attribute name="module" value="true"/>
|
|
||||||
</attributes>
|
|
||||||
</classpathentry>
|
|
||||||
<classpathentry kind="src" path="src"/>
|
|
||||||
<classpathentry kind="output" path="bin"/>
|
|
||||||
</classpath>
|
|
||||||
1
lesson1/.gitignore
vendored
1
lesson1/.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
/bin/
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<projectDescription>
|
|
||||||
<name>lesson1</name>
|
|
||||||
<comment></comment>
|
|
||||||
<projects>
|
|
||||||
</projects>
|
|
||||||
<buildSpec>
|
|
||||||
<buildCommand>
|
|
||||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
|
||||||
<arguments>
|
|
||||||
</arguments>
|
|
||||||
</buildCommand>
|
|
||||||
</buildSpec>
|
|
||||||
<natures>
|
|
||||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
|
||||||
</natures>
|
|
||||||
</projectDescription>
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
eclipse.preferences.version=1
|
|
||||||
encoding/<project>=UTF-8
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
eclipse.preferences.version=1
|
|
||||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
|
|
||||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
|
||||||
org.eclipse.jdt.core.compiler.compliance=21
|
|
||||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
|
||||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
|
||||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
|
||||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
|
||||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
|
||||||
org.eclipse.jdt.core.compiler.release=enabled
|
|
||||||
org.eclipse.jdt.core.compiler.source=21
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
package lesson1;
|
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class StringCheck {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
String[] comment = new String[4];
|
|
||||||
comment[0] = "いまなにしてる?";
|
|
||||||
comment[1] = "何も入力されていません";
|
|
||||||
comment[2] = "投稿文が長すぎます";
|
|
||||||
comment[3] = "↓↓↓↓↓↓ 投稿されました ↓↓↓↓↓↓";
|
|
||||||
|
|
||||||
System.out.println(comment[0]);
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
String text = scanner.next();
|
|
||||||
String str = text.strip();
|
|
||||||
System.out.println("\n" + comment[3]);
|
|
||||||
System.out.println(str);
|
|
||||||
|
|
||||||
boolean isEmpty = str.isEmpty();
|
|
||||||
System.out.println(isEmpty);
|
|
||||||
|
|
||||||
int strLength = str.length();
|
|
||||||
System.out.println(strLength + "文字");
|
|
||||||
|
|
||||||
boolean isRange = strLength > 140;
|
|
||||||
System.out.println(isRange);
|
|
||||||
|
|
||||||
System.out.println(140 - str.length());
|
|
||||||
|
|
||||||
//一日目
|
|
||||||
|
|
||||||
for (String a : comment ) {
|
|
||||||
System.out.println(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
|
|
||||||
int strLength = str.length();
|
|
||||||
System.out.println(strLength + "文字");
|
|
||||||
|
|
||||||
boolean isRange = strLength > 140;
|
|
||||||
System.out.println(isRange);
|
|
||||||
|
|
||||||
System.out.println(140 - str.length());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user