![](/img/trans.png)
[英]I need help figuring out how to stop my program from throwing a NullPointerException
[英]Need help figuring out how to capitalize any lower case "i" read from a text file
CS學生介紹在這里。
在花了很多時間閱讀我的教科書和我教授的幻燈片后,我一直在努力尋找解決方案。
基本上我的程序需要從輸入文件中讀取並進行更正,其中一項更正必須是將所有單個小寫字母“i”大寫並輸出更正后的文件。
我已經處理了輸入/輸出部分,但現在我只是卡住了。
import java.io.*;
import java.util.*;
public class WP {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(new File(args[0])); // first argument is input filename
PrintStream output = new PrintStream(new File(args[1])); // second arg is output filename
stripSpaces(input, output);
capCorrection(input, output);
}
static void stripSpaces(Scanner input, PrintStream output) {
String text = "";
while (input.hasNextLine()) {
text += input.nextLine() + "\n";
}
final int State_INIT = 0;
final int State_SEEN_SPACE = 1;
int state = State_INIT;
for (char c : text.toCharArray()) {
if (state == State_INIT) {
if (c == ' ') {
output.print(c);
state = State_SEEN_SPACE;
} else {
output.print(c);
}
} else if (state == State_SEEN_SPACE) {
if (c != ' ') {
output.print(c);
state = State_INIT;
}
}
}
}
static void capCorrection(input, output);
String text = "";
while (input.hasNextLine()) {
text += input.nextLine() + "\n";
}
final int State_INIT = 0;
final int State_SEEN_I = 1;
int state = State_INIT;
首先問自己你正在嘗試做什么以及如何去做。 就像您需要在兩個空格之間找到字符“i”,這樣您就可以說:如果在“i”之前有一個“”,然后在字符大寫之后有一個“”。
您應該在寫入之前替換該字符。 所以我要做的是讀取文件中的每一行,檢查兩個空格之間的“i”,將其大寫,然后用更正的行寫在該行上
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.