0

Edit:

I've tried to compile all of the code into a single program. Now all I need is to figure out how to return the modified string from the method. What do I have to put in order to achieve this?

Here's the new code:

        public class String_Fixer {

            public static void main(String[] args) {

                String userInput, newString;
                Scanner keyboard = new Scanner(System.in);
                System.out.println("This program takes sentences and capitalizes them for you.");
                System.out.println("Please write a few sentences.");
                System.out.println("");
                userInput = keyboard.nextLine();
                System.out.println("");


                System.out.println("Here is the original sentence:");
                System.out.println(userInput);
                System.out.println("");
                System.out.println("And here is the modified sentence:");
                System.out.println();  
            }

            public static List<Integer> stringFixer(String userInput) {
                List<Integer> indexes = new ArrayList<>();
                int first = 0, offset = 2, index, offset_index;
                while ((index = userInput.indexOf('.', first)) != -1) {
                    offset_index = index + offset;
                    if (offset_index < userInput.length()) {
                        indexes.add(offset_index);
                    }
                    first = index + 1;
                }
                while ((index = userInput.indexOf('?', first)) != -1) {
                    offset_index = index + offset;
                    if (offset_index < userInput.length()) {
                        indexes.add(offset_index);
                    }
                    first = index + 1;
                }
                return indexes;
            }

            public static String CapatilizeChars(List<Integer> indexes, String string) {
                StringBuilder newString = new StringBuilder(string);
                for (int i : indexes) {
                    char capChar = Character.toUpperCase(newString.charAt(i));
                    newString.setCharAt(i, capChar);
                }
                return newString.toString();
            }

        }
Brandon
  • 1
  • 2

1 Answers1

0

Use the Character.toUpperCase(CHARACTER); method to get the upper case variant of a character.

See this post: Indexes of all occurrences of character in a string for information on finding all indexes of a character in a String.

EDIT:

Here's how I would tackle the problem. First get the indexes that need to be changed using this method:

public static List<Integer> getIndexesOf(String regex, String stringToCheck, int offset) {

    List<Integer> indexes = new ArrayList<>();

    // Where to begin each indexof operation
    int first = 0;

    // The direct indexof result
    int index;
    // The indexof result offset by the specified amount
    int offset_index;

    // While we are still getting results from index of, save result under index and loop
    while ((index = stringToCheck.indexOf(regex, first)) != -1) {

        offset_index = index + offset;

        // If the offset index doesn't excede the strings length add it to list
        if (offset_index < stringToCheck.length()) {

            indexes.add(offset_index);

        }

        first = index + 1;

    }

    return indexes;

}

Then capitalize each index using this method:

public static String CapatilizeChars(List<Integer> indexes, String string) {

    StringBuilder newString = new StringBuilder(string);

    // For each index capatilize the specified char
    for (int i : indexes) {

        char capChar = Character.toUpperCase(newString.charAt(i));

        newString.setCharAt(i, capChar);

    }

    return newString.toString();

}

This isn't the fastest way of doing it, but it has the benefit of the method being more useful then a specific capitalize_char_after_regex method. You could easily combine this into one method by taking the code within the for loop of the second method and putting it into the first.

EDIT 2:

Fix for your updated code:

Replace: StringFixer modifiedString = new StringFixer();

With: String modifiedString = StringFixer.CapatilizeChars(StringFixer.getIndexesOf(userInput), userInput);

And remove the regex parameter from the getIndexesOf method since you aren't using them in your application.

The methods I gave you are static, which means they are not associated with an instance of an object. In other words, you don't need to construct the StringFixer class to use them.

Community
  • 1
  • 1
Naberhausj
  • 303
  • 1
  • 9
  • so in the parentheses I should put the indexOf position I specified? – Brandon Mar 13 '17 at 03:06
  • I tried doing this and my program says there is an out of bounds error – Brandon Mar 13 '17 at 03:07
  • No, this method receives a character and returns the upper case variant. So find the indexes and use `String.charAt(index);` to get the character of whatever the index is of the letter you want to change. Then use a `StringBuilder` to replace the character in the string with the new upper case character using the `YOUR_STRING_BUILDER.setCharAt(newChar, index);` – Naberhausj Mar 13 '17 at 03:09
  • could you show this in example code? I'm just a beginner at this – Brandon Mar 13 '17 at 03:11
  • I've got this much: int positionPeriod = modifiedInput.indexOf(". "); while (positionPeriod < modifiedInput.length()) positionPeriod++; Character.toUpperCase(positionPeriod); StringBuilder newString1 = new StringBuilder(modifiedInput); newString1.setCharAt(positionPeriod); – Brandon Mar 13 '17 at 03:16
  • I think it's the newChar part in your stringbuilder comment that's throwing me off. I tried using the previous code you mentioned with Character.toUpperCase(index); but I can't use this. Can you explain? – Brandon Mar 13 '17 at 03:38
  • Just for you, I wrote the code I would use to solve this problem. regex would be where you put whatever your looking for the index of. And for your case, the offset would be 2 – Naberhausj Mar 13 '17 at 04:13
  • so how does regex work if I need to find a period or a question mark? – Brandon Mar 13 '17 at 21:55
  • for a period, you would put a "." in, for a question mark you would put a "?". You could easily modify the code to accept multiple regexes if you wish, or just do it in two steps – Naberhausj Mar 13 '17 at 22:14
  • I tried putting in String "." but it says on the java building program that I'm using: class, interface, or enum expected and after that when I tweaked the code you gave to suit what I need, I get nothing but errors – Brandon Mar 13 '17 at 22:23
  • You are missing a piece of syntax somewhere if you are getting that error. Use an IDE if you are not because they will call this out to you exactly where the problem is. The code I gave you definitely works, if it's not for you it's an error on your part. – Naberhausj Mar 13 '17 at 22:26
  • I'm using netbeans and I essentially copied your code exactly as you put it while changing the variable names to what I need them to be – Brandon Mar 13 '17 at 22:32
  • Did you paste them within the main method? It shouldn't be, put it just below the '}' of the end of the main method. – Naberhausj Mar 13 '17 at 22:33
  • I get several errors when I tried putting in the first method into my code – Brandon Mar 13 '17 at 23:36
  • Nothing I can do about that unless you post you full code. I reiterate that I have tested this code (I even just tried copying and pasting from this website into netbeans) and it works 100%. If it's not working for you it's because you are integrating it into your codebase incorrectly – Naberhausj Mar 13 '17 at 23:42
  • ok. I tried making a new program creating a constructor to send the user prompted string to in order to capitalize the first letters of the sentences, but, when I tried to put the characters to search in the method - at the second time regex appears in your code, I tried to put both "." and "?" but it will not allow that – Brandon Mar 14 '17 at 02:03
  • Brandon, your updated code doesn't resemble at all what I recommend you do. It is effectivelly exactly the same as the first. Simply calling `Character.toUpperCase(positionPeriod);` will not do anything as the purpose of this method is to RETURN a char of upper case value. If aren't yet familiar with methods I highly suggest you learn about them so you know what to do with the code I gave you: https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html – Naberhausj Mar 14 '17 at 02:17
  • I started from scratch and rewrote everything. I hadn't yet put up the new code. – Brandon Mar 14 '17 at 02:20
  • I altered the code so now all I have left is to return the altered string from the method but I'm not sure how to accomplish this – Brandon Mar 14 '17 at 03:02
  • Same as above except get rid of `StringFixer.` FYI this is REALLY not what stack overflow is meant for. I remember being in your situation so I am happy to help you, but please don't start posting every single time you run into a stumbling block in programming. Learning how to find and solve these kinds of problems yourself is in incredibly important part of being a good coder. – Naberhausj Mar 14 '17 at 03:24
  • In actuality I'm not studying to become a programmer - this is one of the required classes for my degree and I'd rather not take it if I could. FYI I got it to work by a completely different method. Thanks for your help anyways – Brandon Mar 14 '17 at 03:52