質問編集履歴
5
追記
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
テキストを読み込みたい
|
test
CHANGED
@@ -1,171 +1 @@
|
|
1
|
-
|
2
|
-
例えば、テキストの●番目から▲番目に書かれた文字を抜き出したり、全文を表示したり。
|
3
|
-
|
4
|
-
|
5
|
-
https://www.sejuku.net/blog/49981
|
6
|
-
このサイトの一番最初の参考にして、一番最初のコードを参考に、以下のようにメモ(SampleTest.txt)に保存したデータを読み込みました。
|
7
|
-
```Java
|
8
|
-
|
9
|
-
import java.io.BufferedReader;
|
10
|
-
import java.io.File;
|
11
|
-
import java.io.FileReader;
|
12
|
-
import java.io.IOException;
|
13
|
-
|
14
|
-
public class Main {
|
15
|
-
|
16
|
-
public static void main(String[] args) {
|
17
|
-
try {
|
18
|
-
// ファイルのパスを指定する
|
19
|
-
File file = new File("c:SampleTest.txt");
|
20
|
-
|
21
|
-
// ファイルが存在しない場合に例外が発生するので確認する
|
22
|
-
if (!file.exists()) {
|
23
|
-
System.out.print("ファイルが存在しません");
|
24
|
-
return;
|
25
|
-
}
|
26
|
-
|
27
|
-
// BufferedReaderクラスのreadLineメソッドを使って1行ずつ読み込み表示する
|
28
|
-
FileReader fileReader = new FileReader(file);
|
29
|
-
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
30
|
-
String str;
|
31
|
-
while ((str = bufferedReader.readLine()) != null) {
|
32
|
-
System.out.println(str);
|
33
|
-
}
|
34
|
-
|
35
|
-
// 最後にファイルを閉じてリソースを開放する
|
36
|
-
bufferedReader.close();
|
37
|
-
|
38
|
-
} catch (IOException e) {
|
39
|
-
e.printStackTrace();
|
40
|
-
}
|
41
|
-
}
|
42
|
-
}
|
43
|
-
|
44
|
-
```
|
45
|
-
|
46
|
-
しかし、この読み込んだdataを使って、式をかけません。
|
47
|
-
実際に書いたコードはこれです。(2つ)↓
|
48
|
-
```Java
|
49
|
-
public class Main {
|
50
|
-
public static void main(String[] args) {
|
51
|
-
|
52
|
-
Program pro = new Program();
|
53
|
-
pro.start();
|
54
|
-
}
|
55
|
-
}
|
56
|
-
```
|
57
|
-
|
58
|
-
```java
|
59
|
-
|
60
|
-
import java.io.BufferedReader;
|
61
|
-
import java.io.File;
|
62
|
-
import java.io.FileReader;
|
63
|
-
import java.io.IOException;
|
64
|
-
import java.util.InputMismatchException;
|
65
|
-
|
66
|
-
public class Pro {
|
67
|
-
|
68
|
-
public void start() {
|
69
|
-
try {
|
70
|
-
//ファイルパス指定
|
71
|
-
File file = new File("c:SampleTest.txt");
|
72
|
-
|
73
|
-
// ファイルが存在しない場合の例外
|
74
|
-
if (!file.exists()) {
|
75
|
-
System.out.print("ファイルが存在しません");
|
76
|
-
return;
|
77
|
-
}
|
78
|
-
|
79
|
-
//BufferedReaderクラスのreadLineメソッド
|
80
|
-
FileReader fileReader = new FileReader(file);
|
81
|
-
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
82
|
-
String str= bufferedReader.readLine();
|
83
|
-
while ((str) != null) {
|
84
|
-
System.out.println(str);
|
85
|
-
}
|
86
|
-
// ファイルを閉じてリソースを開放
|
87
|
-
bufferedReader.close();
|
88
|
-
|
89
|
-
} catch (IOException e) {
|
90
|
-
e.printStackTrace();
|
91
|
-
}
|
92
|
-
|
93
|
-
int st = check("[この文章で良いですか?] 0:Yes 1:No");
|
94
|
-
//while(true) {
|
95
|
-
if(st == 0) {
|
96
|
-
System.out.println("【メニュー】");
|
97
|
-
System.out.println("[A] 文字数をカウントする ");
|
98
|
-
System.out.println("[B] 検索した文字の出力");
|
99
|
-
System.out.println("メニューを選択してください");
|
100
|
-
String menu = new java.util.Scanner(System.in).nextLine();
|
101
|
-
|
102
|
-
if (menu == "A") {
|
103
|
-
gameA(str);
|
104
|
-
} else if (menu == "B") {
|
105
|
-
gameB(str);
|
106
|
-
}
|
107
|
-
|
108
|
-
}else {
|
109
|
-
System.out.println("終了します");
|
110
|
-
}
|
111
|
-
}
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
//文字数カウント
|
116
|
-
void gameA(str) {
|
117
|
-
long total = str.chars().filter(ch -> ch != ' ').count();
|
118
|
-
System.out.println(total);
|
119
|
-
}
|
120
|
-
|
121
|
-
//検索した文字を出力
|
122
|
-
void gameB(str) {
|
123
|
-
System.out.println("検索したい文字を入力");
|
124
|
-
String input = new java.util.Scanner(System.in).nextLine();
|
125
|
-
|
126
|
-
int num = 0;
|
127
|
-
for(int i = 0; i < str.length(); i++) {
|
128
|
-
num = str.indexOf(input, i);
|
129
|
-
if(num != -1) {
|
130
|
-
System.out.println(data.substring(num , num + input.length() ));
|
131
|
-
i = num;
|
132
|
-
}
|
133
|
-
}
|
134
|
-
}
|
135
|
-
|
136
|
-
|
137
|
-
private int check(String text) {
|
138
|
-
int input = 0;
|
139
|
-
while (true) { //間違いを何回も修正させるため
|
140
|
-
System.out.println(text);
|
141
|
-
try {
|
142
|
-
input = new java.util.Scanner(System.in).nextInt();
|
143
|
-
if (input < 0 || input > 1) {
|
144
|
-
System.out.println("入力は0または1で入力してください");
|
145
|
-
} else {
|
146
|
-
break;
|
147
|
-
}
|
148
|
-
} catch (InputMismatchException e) {
|
149
|
-
System.out.println("入力は半角数字で入力してください");
|
150
|
-
}
|
151
|
-
}
|
152
|
-
return input;
|
153
|
-
}
|
154
|
-
}
|
155
|
-
```
|
156
|
-
> SampleTest.txt
|
157
|
-
The catch is that both the thin wires and the curved surface on the back are charged with 20,000 volts, one is positive volts, while the other is negative, respectively. The electrons from the front are removed from the nitrogen molecules in the air and thus produce ions. They accelerate in the back and produce an ionic wind, which helps the plane fly.
|
158
|
-
|
159
|
-
|
160
|
-
としたい(textの中身はなんでもいいです)ですが、
|
161
|
-
if (menu == "A") {gameA(str);}のstrまたは、void gameA(str) のstrにエラーがでたり、
|
162
|
-
|
163
|
-
以下のようなエラーが出てしまいます
|
164
|
-
> 「Exception in thread "main" java.lang.Error: Unresolved compilation problems:
|
165
|
-
data を変数に解決できません
|
166
|
-
構文エラーがあります。"... VariableDeclaratorId" を挿入して FormalParameterList を完了してください data を解決できません」
|
167
|
-
|
168
|
-
|
169
|
-
初めてbufferedReaderを扱うため、そもそもbufferedReaderを使ったらその後は処理(ここではgameAやgameB)ができないのか(別の方法を使って読み込むべきなのか)、strを定義する位置や方法が悪いのか、わかりません。
|
170
|
-
|
171
|
-
初めての質問でいろいろと足りないところがあったり、初歩的な質問かもしれませんが、わかる方いらっしゃいましたら、よろしくお願いします!
|
1
|
+
string builderをつかって、テキストのテキストの読み読み込みを
|
4
コードを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -44,25 +44,128 @@
|
|
44
44
|
```
|
45
45
|
|
46
46
|
しかし、この読み込んだdataを使って、式をかけません。
|
47
|
-
|
47
|
+
実際に書いたコードはこれです。(2つ)↓
|
48
|
-
```
|
48
|
+
```Java
|
49
|
-
|
49
|
+
public class Main {
|
50
|
-
|
50
|
+
public static void main(String[] args) {
|
51
|
-
game();
|
52
51
|
|
53
|
-
……
|
54
|
-
|
55
|
-
void game(String str){
|
56
|
-
// 左から3文字目を切り出し
|
57
|
-
|
52
|
+
Program pro = new Program();
|
58
|
-
|
53
|
+
pro.start();
|
59
|
-
|
54
|
+
}
|
55
|
+
}
|
60
56
|
```
|
61
57
|
|
62
|
-
|
58
|
+
```java
|
63
|
-
> 「Exception in thread "main" java.lang.Error: Unresolved compilation problems:
|
64
|
-
型 SentenceProgram のメソッド game(String) は引数 () に適用できません」
|
65
59
|
|
66
|
-
|
60
|
+
import java.io.BufferedReader;
|
61
|
+
import java.io.File;
|
62
|
+
import java.io.FileReader;
|
63
|
+
import java.io.IOException;
|
64
|
+
import java.util.InputMismatchException;
|
67
65
|
|
68
|
-
|
66
|
+
public class Pro {
|
67
|
+
|
68
|
+
public void start() {
|
69
|
+
try {
|
70
|
+
//ファイルパス指定
|
71
|
+
File file = new File("c:SampleTest.txt");
|
72
|
+
|
73
|
+
// ファイルが存在しない場合の例外
|
74
|
+
if (!file.exists()) {
|
75
|
+
System.out.print("ファイルが存在しません");
|
76
|
+
return;
|
77
|
+
}
|
78
|
+
|
79
|
+
//BufferedReaderクラスのreadLineメソッド
|
80
|
+
FileReader fileReader = new FileReader(file);
|
81
|
+
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
82
|
+
String str= bufferedReader.readLine();
|
83
|
+
while ((str) != null) {
|
84
|
+
System.out.println(str);
|
85
|
+
}
|
86
|
+
// ファイルを閉じてリソースを開放
|
87
|
+
bufferedReader.close();
|
88
|
+
|
89
|
+
} catch (IOException e) {
|
90
|
+
e.printStackTrace();
|
91
|
+
}
|
92
|
+
|
93
|
+
int st = check("[この文章で良いですか?] 0:Yes 1:No");
|
94
|
+
//while(true) {
|
95
|
+
if(st == 0) {
|
96
|
+
System.out.println("【メニュー】");
|
97
|
+
System.out.println("[A] 文字数をカウントする ");
|
98
|
+
System.out.println("[B] 検索した文字の出力");
|
99
|
+
System.out.println("メニューを選択してください");
|
100
|
+
String menu = new java.util.Scanner(System.in).nextLine();
|
101
|
+
|
102
|
+
if (menu == "A") {
|
103
|
+
gameA(str);
|
104
|
+
} else if (menu == "B") {
|
105
|
+
gameB(str);
|
106
|
+
}
|
107
|
+
|
108
|
+
}else {
|
109
|
+
System.out.println("終了します");
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
//文字数カウント
|
116
|
+
void gameA(str) {
|
117
|
+
long total = str.chars().filter(ch -> ch != ' ').count();
|
118
|
+
System.out.println(total);
|
119
|
+
}
|
120
|
+
|
121
|
+
//検索した文字を出力
|
122
|
+
void gameB(str) {
|
123
|
+
System.out.println("検索したい文字を入力");
|
124
|
+
String input = new java.util.Scanner(System.in).nextLine();
|
125
|
+
|
126
|
+
int num = 0;
|
127
|
+
for(int i = 0; i < str.length(); i++) {
|
128
|
+
num = str.indexOf(input, i);
|
129
|
+
if(num != -1) {
|
130
|
+
System.out.println(data.substring(num , num + input.length() ));
|
131
|
+
i = num;
|
132
|
+
}
|
133
|
+
}
|
134
|
+
}
|
135
|
+
|
136
|
+
|
137
|
+
private int check(String text) {
|
138
|
+
int input = 0;
|
139
|
+
while (true) { //間違いを何回も修正させるため
|
140
|
+
System.out.println(text);
|
141
|
+
try {
|
142
|
+
input = new java.util.Scanner(System.in).nextInt();
|
143
|
+
if (input < 0 || input > 1) {
|
144
|
+
System.out.println("入力は0または1で入力してください");
|
145
|
+
} else {
|
146
|
+
break;
|
147
|
+
}
|
148
|
+
} catch (InputMismatchException e) {
|
149
|
+
System.out.println("入力は半角数字で入力してください");
|
150
|
+
}
|
151
|
+
}
|
152
|
+
return input;
|
153
|
+
}
|
154
|
+
}
|
155
|
+
```
|
156
|
+
> SampleTest.txt
|
157
|
+
The catch is that both the thin wires and the curved surface on the back are charged with 20,000 volts, one is positive volts, while the other is negative, respectively. The electrons from the front are removed from the nitrogen molecules in the air and thus produce ions. They accelerate in the back and produce an ionic wind, which helps the plane fly.
|
158
|
+
|
159
|
+
|
160
|
+
としたい(textの中身はなんでもいいです)ですが、
|
161
|
+
if (menu == "A") {gameA(str);}のstrまたは、void gameA(str) のstrにエラーがでたり、
|
162
|
+
|
163
|
+
以下のようなエラーが出てしまいます
|
164
|
+
> 「Exception in thread "main" java.lang.Error: Unresolved compilation problems:
|
165
|
+
data を変数に解決できません
|
166
|
+
構文エラーがあります。"... VariableDeclaratorId" を挿入して FormalParameterList を完了してください data を解決できません」
|
167
|
+
|
168
|
+
|
169
|
+
初めてbufferedReaderを扱うため、そもそもbufferedReaderを使ったらその後は処理(ここではgameAやgameB)ができないのか(別の方法を使って読み込むべきなのか)、strを定義する位置や方法が悪いのか、わかりません。
|
170
|
+
|
171
|
+
初めての質問でいろいろと足りないところがあったり、初歩的な質問かもしれませんが、わかる方いらっしゃいましたら、よろしくお願いします!
|
3
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -61,7 +61,7 @@
|
|
61
61
|
|
62
62
|
としたいですが、以下のようなエラーが出てしまいます
|
63
63
|
> 「Exception in thread "main" java.lang.Error: Unresolved compilation problems:
|
64
|
-
型 SentenceProgram のメソッド a
|
64
|
+
型 SentenceProgram のメソッド game(String) は引数 () に適用できません」
|
65
65
|
|
66
66
|
初めて扱うため、そもそもbufferedReaderを使ったらその後は処理等できないのか(別のものを使って読み込むべきなのか)、strを定義する位置が悪いのか、わかりません。
|
67
67
|
|
2
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
https://www.sejuku.net/blog/49981
|
6
|
-
このサイトの一番最初の参考にして、一番最初のコードを参考に、以下のようにメモに保存したデータを読み込みました。
|
6
|
+
このサイトの一番最初の参考にして、一番最初のコードを参考に、以下のようにメモ(SampleTest.txt)に保存したデータを読み込みました。
|
7
7
|
```Java
|
8
8
|
|
9
9
|
import java.io.BufferedReader;
|
1
誤字変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -61,7 +61,7 @@
|
|
61
61
|
|
62
62
|
としたいですが、以下のようなエラーが出てしまいます
|
63
63
|
> 「Exception in thread "main" java.lang.Error: Unresolved compilation problems:
|
64
|
-
型 SentenceProgram のメソッド actionA(String) は引数 () に適用できません」
|
64
|
+
型 SentenceProgram のメソッド actionA(String) は引数 () に適用できません」
|
65
65
|
|
66
66
|
初めて扱うため、そもそもbufferedReaderを使ったらその後は処理等できないのか(別のものを使って読み込むべきなのか)、strを定義する位置が悪いのか、わかりません。
|
67
67
|
|