Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sentence capitlizer using toUpperCase

801497Feb 6 2009 — edited Feb 6 2009
Hello, I need to convert the first letter of each sentence to a capital letter if the user inputs a lower case letter. I'm having a difficult time trying to figure out where I'm going wrong at so here's my pseudo code.
import java.util.Scanner;

public class Cap {

    public static void main(String[] args) {
        
       Scanner keyboard = new Scanner(System.in); 
         
       String yourSentence = "Please enter a string";
       System.out.println(Cap(yourSentence));
       yourSentence = keyboard.next();
       
       //yourSentence = JOptionPane.showInputDialog(Cap(yourSentence)); (this was my original display, but I decided to use the System.out instead)

      }

    // method to cap 1st letter of the sentence
    public static String Cap(String str) {
        
        StringBuffer temp = new StringBuffer(str);
        
        int[] numberOfCaps = new int[1];
        
      for (int i=1; i<numberOfCaps.length; i++) {
          numberOfCaps[i] = (int)(Math.random() * temp.length());
    }
      for(int x=1; x<numberOfCaps.length; x++) {
        temp.setCharAt(numberOfCaps[x], Character.toUpperCase(str.charAt(numberOfCaps[x])));
      }
        return temp.toString();
    }   

}
Edited by: pbsacct_1 on Feb 5, 2009 9:40 PM

Edited by: pbsacct_1 on Feb 5, 2009 9:40 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 6 2009
Added on Feb 6 2009
35 comments
402 views