Hi guys,
I'm new and I'm totally stuck on a question for class! Basically we have to get the user to enter a few sentences and then change the first letter of every sentence to a capital version. I'll post my code below.
(I'm not asking for the answer just some help would be nice!)

import java.util.*;
public class CH10Q3
{
    public static void main(String[] args)
    {
        String input;
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("Please enter a couple sentences");
        input = keyboard.nextLine();
        int periods=0;
        
        while(periods!=-1)
        {
            System.out.println("While Start");
            periods = input.indexOf('.', periods+3);
            System.out.println(periods);
            if(periods!=-1)
                Character.toUpperCase(input.charAt(periods));
            System.out.println("While End");
        }
        System.out.println(input);
    }
}

Dani AI

Generated

Good debugging by — spotting String immutability and the bounds error are the exact lessons this exercise is meant to teach. ’s encouragement is also on point. The final approach posted works for straightforward input, but it assumes a single space after every period and only handles '.'; a few small changes make the routine more robust and simpler to maintain.

A compact, single-pass strategy avoids index arithmetic and out-of-range problems: iterate the characters once, remember when a sentence end has been seen, and uppercase the first alphabetic character that follows. This keeps spacing and punctuation intact and copes with quotes or multiple spaces between sentences.

StringBuilder sb = new StringBuilder(input);
boolean capitalizeNext = true;

for (int i = 0; i < sb.length(); i++) {
    char c = sb.charAt(i);
    if (capitalizeNext && Character.isLetter(c)) {
        sb.setCharAt(i, Character.toUpperCase(c));
        capitalizeNext = false;
    }
    if (c == '.' || c == '!' || c == '?') {
        capitalizeNext = true;
    }
}

System.out.println(sb.toString());

Notes and caveats: prefer StringBuilder (single-threaded) over StringBuffer; guard against empty input by scanning for the first letter instead of blindly touching index 0; consider other sentence terminators and quoted punctuation; and remember locale issues (Turkish i/İ) — for full international correctness use locale-aware uppercasing or Java’s BreakIterator.getSentenceInstance(...) to locate sentence boundaries more reliably. Test with edge cases (empty string, leading whitespace, multiple spaces, newline-separated sentences, ellipses and common abbreviations) to confirm behavior.

Okay heres a small update, I am converting my string into a buffered string so its mutable. Here is the code I have now, only problem is it goes out of range!

import java.util.*;
public class CH10Q3
{
    public static void main(String[] args)
    {
        String input;
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("Please enter a couple sentences");
        input = keyboard.nextLine();
        StringBuffer input2 = new StringBuffer(input);
        int periods=0;
        
        while(periods!=-1)
        {
            System.out.println("While Start");
            periods = input2.indexOf(".", periods+2);
            System.out.println(periods);
            if(periods!=-1)
            {
                Character.toUpperCase(input2.charAt(periods));
                System.out.println(Character.toUpperCase(input2.charAt(periods+2)));
            }
            System.out.println("While End");
        }
        System.out.println(input2);
    }
}

Any suggestions would be lovely.

Ah well I figured it out on my own.
First problem was I was trying to modify a immutable object.
Second was I was going waaay out of range.

Heres the finaly code

import java.util.*;
public class CH10Q3
{
    public static void main(String[] args)
    {
        String input;
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("Please enter a couple sentences");
        input = keyboard.nextLine();
        StringBuffer input2 = new StringBuffer(input);
        int periods=0;
        
        input2.setCharAt(0, Character.toUpperCase(input2.charAt(0)));
        
        while(periods!=-1)
        {
            periods = input2.indexOf(".", periods+2);
            if((periods!=-1)&&(periods<(input.length()-2)))
            {
                input2.setCharAt(periods+2, Character.toUpperCase(input2.charAt(periods+2)));
            }
        }
        System.out.println(input2);
    }
}

(This post is like my "I did it WOOO HOOOOO" )

That is what often happens if you not sitting on your back-side and waiting for others to solve your issue...
Nicely done

Haha, thanks. Well I see so many people asking for the answers, and that's not going to help me with my Comp. Sci. Degree... plus I want to be able to write nice software without having to ask people around me constantly for help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.