简体   繁体   中英

Need help figuring out how to capitalize any lower case "i" read from a text file

Intro to CS student here.

I've been struggling to find a solution after spending a lot of time reading my text book and reviewing my professors slides.

Basically my program needs to read from an input file and make corrections, one of the corrections must be capitalizing all individual lowercase "i"s and outputting the corrected file.

I have the input/output part taken care of but now I'm just plain stuck.

    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;

Start off with asking yourself with what you are trying to do and how you can do it. Like you will need to locate the charachter "i" between two spaces so you can say: IF there is a " " befor "i" AND a " " after then character the capitalize.

You should replace the character before writing it. So What I would do is read each line in the file, check for "i" between two spaces, capitalize it, and then write over the line with the corrected line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM