For some reason my for loop is not terminating in my CapitalizeFirstSentence method. I set a breakpoint at that line and the condition (i != -1) is unmet, so the loop should terminate, but it doesn't!
It works when I use (i > 0) for the condition.
I'm not sure what's going on here.
import javax.swing.JOptionPane;
public class SentenceCapitalizer {
//Main Method
public static void main(String[] args) {
String input; //creates a String to hold keyboard input
//Prompt the user to enter a String using JOptionPane and set it equal to input
input = JOptionPane.showInputDialog("Enter a string. ");
//Display the new String with the first letter of each sentenced capitalized
JOptionPane.showMessageDialog(null, CapitalizeFirstSentence(input));
//Exit the program
System.exit(0);
}
//Capitalize first letter of each sentence
public static String CapitalizeFirstSentence(String in)
{
//Creates a StringBuilder object initiralized to the String argument "in"
StringBuilder temp = new StringBuilder(in);
//Capitalize first letter of the string if string length is > 0
if (temp.length() > 0)
{
temp.setCharAt(0, Character.toUpperCase(temp.charAt(0)));
}
//sets i equal to index of the space,
//keep capitalizing first letters of each sentence (loops each time it capitlizes a letter)
//until very end of the String
for (int i = temp.indexOf(". ")+1; i != -1; i++)
{
//Checks for extra spaces and moves index to first character of next sentence
while (i < temp.length() && temp.charAt(i) == ' ')
{
i++;
}
//Capitalize character
temp.setCharAt(i, Character.toUpperCase(temp.charAt(i)));
//Index the end of the sentence
i = temp.indexOf(". ", i);
}
//Convert temp to a String and return our new first-sentenced-capitalized String
return temp.toString();
}
}
It will not stop because you use "or" || in your while statement. so If one expression at least is true then the loop will keep executing. You have to use "and" && instead.
The issue with your while loop not closing is because you have an embedded for loop in your code. What happens, is your code will enter the while loop, because while(test) will result in true . Then, your code will enter the for loop. Inside of your for loop, you have the code looping from 1-10.
break terminates the execution of a for or while loop. Statements in the loop after the break statement do not execute. In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop.
The break statement is used to terminate the loop immediately. The continue statement is used to skip the current iteration of the loop. break keyword is used to indicate break statements in java programming. continue keyword is used to indicate continue statement in java programming.
First, it is not a good idea to modify the loop-controlling variable inside a for loop - it is quite hard to read and understand such code, and is prone to errors.
Now, to your example:
for (int i = temp.indexOf(". ")+1; i != -1; i++)
This means:
i
to temp.indexOf(". ")+1
, which is always >= 0i == -1
i
by 1So:
i = temp.indexOf(". ", i);
, which is >= -1i
will be incremented by 1, so it will now be >= 0i
is always >= 0, it will never meet the condition i == -1
and thus will never terminateThis line: for (int i = temp.indexOf(". ")+1; i != -1; i++)
initializes i to be the result of indexOf + 1. IndexOf gives -1 if there is no hit, but you always add 1 to that during initialization, so it'll never be smaller than 0.
Using i > 0
seems perfectly fine there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With