Click to See Complete Forum and Search --> : almost done...please help?


mbp123
October 16th, 2007, 08:28 PM
so i need to write this program that will take a sentence inputted by the user such as "this IS THe JaVA senTeNce" and convert it to "This Is The Java Sentence". also it must tally up all the spaces, capital, and lowercase letters and print those out at the end.

I am very new to java, so I am just using basic loops, and if/else statements. Here is the code I have thus far, but I seem to have hit a wall. Any help is greatly appreciated. Thanks

import javax.swing.JOptionPane;

public class asgn3{
public static void main( String[] args )
{
String sentence = JOptionPane.showInputDialog(null,
"Enter a sentence");
boolean first_letter = true;
char this_char;
int upper = 0;
int lower = 0;
int space = 0;
int other = 0;
int counter = 0;
int total = sentence.length();//declared all the variables


while (counter < total) {

this_char = sentence.charAt(counter);
if (first_letter == true) { // capitalize all first letter of each word
if (this_char >= 97 && this_char <= 122) {

this_char -= 32; // convert to uppercase
System.out.print (this_char);
upper++; //bump the count of upper case characters
first_letter == false;// reset the flag so next loop won't look at first letter.
}


}

else { //
if (this_char >= 65 && this_char <= 122) {// if this is an upper case letter
this_char += 32;// convert it to lower case.
lower++; // update count of lower case letters.
}


// print out the letter.
else {
if (this_char == 32) // if this is a blank

// set the flag that tells the loop to capitalize the first letter
space++;// update count of blanks



}


// print the character withut a line return.

counter++;

}


System.out.println("Total: " + total);
System.out.println("Upper: " + upper);
System.out.println("Lower: " + lower);
System.out.println("Space: " + space);
System.out.println("Other: " + other);
System.out.println("Total: " + total);


} // end main
} // end of program


i think i know what to do, as i wrote it in the comments, but im not really sure how to actually do it. any help is greatly appreciated. thanks!

dlorde
October 17th, 2007, 04:51 AM
Here is the code I have thus far, but I seem to have hit a wall.what wall?
[quote]i think i know what to do, as i wrote it in the comments, but im not really sure how to actually do it. QUOTE]It's good that there are comments, but they don't say what the problem is. I'm not a mind reader - what is it you're having trouble with?

A prudent question is one-half of wisdom...
F. Bacon