Hello I have a question from starting out with java 6th edition on chapter 9 pro
ID: 3596270 • Letter: H
Question
Hello I have a question from starting out with java 6th edition on chapter 9 programming challenge 3 (3PC) I copied the program that was their to see if it was correct but when I ran it and put " hello. my name is joe. what is your name?" as an input it came out "Hello. My Name Is Joe. What Is Your Name?" everything was capitalized.
The problem asks to create a program that capitalizes only the first letter in each sentence not every word . so I was asking if you can help me with the solution to this program.
I may have inputted it wrong also I didn't know what symbol that was at the while statement within the while statement that says while(i < temp.length() && temp.charAt(i) == ' ') i didnt know what the ' ' was if you see the solution in the cheggs textbook solutions
basically im asking for a program that capitalizes the first letter of each sentance. not every word of each sentance.
thank you for your time
this is what i have so far from the textbook solution and everything comes out first letter capitalized
import javax.swing.JOptionPane; //importing JOptionPane
public class sentanceCapitalizer
{
public static void main(String[] args) //main function
{
String input; //declaring string variable
int words; //declaring int variable
//inputting string
input=JOptionPane.showInputDialog("Enter string: ");
JOptionPane.showMessageDialog(null, sentanceCap(input));
System.exit(0); //exitting program
}
public static String sentanceCap(String str)
{
//Loop variable
int i;
StringBuilder temp = new StringBuilder(str);
//capitalizes the first letter
if(temp.length() > 0)
temp.setCharAt (0, Character.toUpperCase(temp.charAt(0)));
//get the of the first period followed by a space
i = temp.indexOf(" ");
while(i!=-1)
{
//increments i up by one
i++;
//find the first character
while(i < temp.length() && temp.charAt(i) == ' ')
i++;
//capitalize the character
temp.setCharAt(i, Character.toUpperCase(temp.charAt(i)));
i = temp.indexOf(" ", i);
}
return temp.toString();
}
}
Explanation / Answer
Hi, I have modified the given program such that it only capatilize first letter of each sentence.
import javax.swing.JOptionPane; //importing JOptionPane
public class sentenceCapatilize
{
public static void main(String[] args) //main function
{
String input; //declaring string variable
int words; //declaring int variable
//inputting string
input=JOptionPane.showInputDialog("Enter string: ");
JOptionPane.showMessageDialog(null, sentanceCap(input));
System.exit(0); //exitting program
}
public static String sentanceCap(String str)
{
//Loop variable
int i;
StringBuilder temp = new StringBuilder(str);
//capitalizes the first letter
if(temp.length() > 0)
temp.setCharAt (0, Character.toUpperCase(temp.charAt(0)));
//get the of the first period followed by a space
i = temp.indexOf(".");
while(i!=-1)
{
//increments i up by one
i++;
//find the first character
while(i < temp.length() && temp.charAt(i) == ' ')
i++;
//capitalize the character
if(i < temp.length())
temp.setCharAt(i, Character.toUpperCase(temp.charAt(i)));
i = temp.indexOf(".", i);
}
return temp.toString();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.