teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

4

隠蔽

2018/04/24 06:58

投稿

fuzzball
fuzzball

スコア16733

answer CHANGED
@@ -1,33 +1,3 @@
1
1
  1. String.splitで分割。
2
2
  1. キャピタライズは面倒臭いので**Apache Commons Text**の[WordUtilsクラス](https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/WordUtils.html)のキャピタライズ関連のメソッドを使う。
3
- 1. String.formatで整形。
3
+ 1. String.formatで整形。
4
-
5
- ---
6
-
7
- ```java
8
- String lines[] = {
9
- "apple 100 5",
10
- "banana 30 10",
11
- "curry rice 500 1",
12
- };
13
- for (String line: lines) {
14
- //分割
15
- String[] splitted = line.split(" ", 0);
16
- int length = splitted.length;
17
-
18
- //名前
19
- String name = String.join(" ", Arrays.copyOfRange(splitted, 0, length-2));
20
- /* キャピタライズは省略 */
21
- //数量
22
- int quantity = Integer.parseInt(splitted[length-1]);
23
- //単価
24
- int price = Integer.parseInt(splitted[length-2]);
25
-
26
- //整形
27
- String formatted = String.format("%-10s %6s %6d %7d", name, "$"+(double)price, quantity, price*quantity);
28
- System.out.println(formatted);
29
- }
30
- //=> apple $100.0 5 500
31
- //=> banana $30.0 10 300
32
- //=> curry rice $500.0 1 500
33
- ```

3

丸投げ

2018/04/24 06:58

投稿

fuzzball
fuzzball

スコア16733

answer CHANGED
@@ -1,3 +1,33 @@
1
1
  1. String.splitで分割。
2
2
  1. キャピタライズは面倒臭いので**Apache Commons Text**の[WordUtilsクラス](https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/WordUtils.html)のキャピタライズ関連のメソッドを使う。
3
- 1. String.formatで整形。
3
+ 1. String.formatで整形。
4
+
5
+ ---
6
+
7
+ ```java
8
+ String lines[] = {
9
+ "apple 100 5",
10
+ "banana 30 10",
11
+ "curry rice 500 1",
12
+ };
13
+ for (String line: lines) {
14
+ //分割
15
+ String[] splitted = line.split(" ", 0);
16
+ int length = splitted.length;
17
+
18
+ //名前
19
+ String name = String.join(" ", Arrays.copyOfRange(splitted, 0, length-2));
20
+ /* キャピタライズは省略 */
21
+ //数量
22
+ int quantity = Integer.parseInt(splitted[length-1]);
23
+ //単価
24
+ int price = Integer.parseInt(splitted[length-2]);
25
+
26
+ //整形
27
+ String formatted = String.format("%-10s %6s %6d %7d", name, "$"+(double)price, quantity, price*quantity);
28
+ System.out.println(formatted);
29
+ }
30
+ //=> apple $100.0 5 500
31
+ //=> banana $30.0 10 300
32
+ //=> curry rice $500.0 1 500
33
+ ```

2

こっそりリンク修正。

2018/04/24 06:57

投稿

fuzzball
fuzzball

スコア16733

answer CHANGED
@@ -1,3 +1,3 @@
1
1
  1. String.splitで分割。
2
- 1. キャピタライズは面倒臭いので**Apache Commons Text**の[WordUtilsクラス](https://commons.apache.org/proper/commons-text/javadocs/api-1.2/org/apache/commons/text/WordUtils.html)のキャピタライズ関連のメソッドを使う。
2
+ 1. キャピタライズは面倒臭いので**Apache Commons Text**の[WordUtilsクラス](https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/WordUtils.html)のキャピタライズ関連のメソッドを使う。
3
3
  1. String.formatで整形。

1

順番。

2018/04/24 06:32

投稿

fuzzball
fuzzball

スコア16733

answer CHANGED
@@ -1,3 +1,3 @@
1
1
  1. String.splitで分割。
2
+ 1. キャピタライズは面倒臭いので**Apache Commons Text**の[WordUtilsクラス](https://commons.apache.org/proper/commons-text/javadocs/api-1.2/org/apache/commons/text/WordUtils.html)のキャピタライズ関連のメソッドを使う。
2
- 1. String.formatで整形。
3
+ 1. String.formatで整形。
3
- 1. キャピタライズは面倒臭いので、Apache Commons Textの[WordUtilsクラス](https://commons.apache.org/proper/commons-text/javadocs/api-1.2/org/apache/commons/text/WordUtils.html)のキャピタライズ関連のメソッドを使うのが簡単かと。