-5

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;
Roushan
  • 4,074
  • 3
  • 21
  • 38
MK1986
  • 9
  • 1
  • 3
    We're stuck too. Paste some code, and we might know what's going on. – ergonaut Nov 08 '15 at 02:45
  • 1
    please post your code if there is any – bakki Nov 08 '15 at 02:46
  • 1
    Check [Use Java and RegEx to convert casing in a string](http://stackoverflow.com/questions/2770967/use-java-and-regex-to-convert-casing-in-a-string) – sam Nov 08 '15 at 02:47
  • 2
    There are many ways to do this, the fact that you don't even outline the approach you took that isn't working makes me think you haven't tried anything and are just looking for someone to do your homework. I'd love it if you proved me wrong by editing your question into something more like a specific programming question... – John3136 Nov 08 '15 at 02:49
  • Ok so my question is how do I find individual "i"s from a text file and capitalize them? I didn't save my earlier attempts that's why it's not in my provided code and I'm just look for some direction and not a copy/paste answer. – MK1986 Nov 08 '15 at 03:01
  • You can write anything in the `capCorrection()` method, and it won't work, because `stripSpaces()` already consumed the input, so `capCorrection()` won't see anything at all. – Andreas Nov 08 '15 at 03:50
  • Two ways to uppercase an `i`: A) `if (c == 'i') { c = 'I'; }` B) `if (c == 'i') { c = Character.toUpperCase(c); }` – Andreas Nov 08 '15 at 03:53

1 Answers1

0

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

JavaFox
  • 61
  • 14
  • Ok so something like: for string " i " convert to uppercase? – MK1986 Nov 08 '15 at 03:03
  • yes but the string will be each line in the file so take each line and put it into an character array and go through each one and find the single "i"s and then convert to uppercase. Then take the corrected line and write it to the file – JavaFox Nov 08 '15 at 03:08
  • You are going to write over the whole file by going through each character in each line and changing the case of the "i" . Just make sure its a character between two spaces. – JavaFox Nov 08 '15 at 03:12