Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
hmm. lots of complicated code with bounds checking and that kind of shizzle?

sure but why not this?

Why not? Because you don't get to those concepts in the first semester of an Intro to Java programming class. No StringBuffers and no for-each iterators. At least not yet.
 
Why not? Because you don't get to those concepts in the first semester of an Intro to Java programming class. No StringBuffers and no for-each iterators. At least not yet.

Pure semantics, then this would be better?

String sentence = "whY dId tHe Duck Eat a B c sTuFF 111!@#$%^&*()";
String tmp = "";

String[] words = sentence.split(" ");

for (int i = 0; i < words.length; i++) {
tmp += words.substring(0, 1).toUpperCase();
tmp += words.substring(1).toLowerCase();
tmp += " ";
}

sentence = tmp.trim();

System


I thought people didn't come here to get their homework done but to learn?

if a for is also too much for an introductory course, here is the one that doesn't even need a loop, doesn't need bounds checking, just basic string ops:

public static void main(String[] args) {

String sentence = "whY dId tHe Duck Eat a B c sTuFF!@#$%^&*()";

System.out.println(fixSentence(sentence, true));

}

public static String fixSentence(String string, boolean capitalize){

if (string.length() == 0){ return ""; }

String c = string.substring(0, 1);

if (capitalize){

return c.toUpperCase() + fixSentence(string.substring(1), c.equals(" "));

} else {

return c.toLowerCase() + fixSentence(string.substring(1), c.equals(" "));

}

}
 
i dont know what tmp is although i do appreciate the help and numerous posts. I need to use TextString.charAt(position) and TextString.length()
 
Pure semantics, then this would be better?



I thought people didn't come here to get their homework done but to learn?

if a for is also too much for an introductory course, here is the one that doesn't even need a loop, doesn't need bounds checking, just basic string ops:

Recursive functions. Now that's a step in the right direction. Not.
 
It never will meet the condition to go out of bounds and crash the program. You will never have a string that ends with a space which is required to meet the condition, since a.trim removes all extra spaces.
 
I thought people didn't come here to get their homework done but to learn?

I guess that's the idea. It's easy to post code with little explanation that does it in less lines, or in a smarter way than the last person, but it doesn't actually help the OP. On the other hand, Todd has gone out of his way to help the OP.

b e n
 
Code:
import javax.swing.JOptionPane;

public class asgn3comments{
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();


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 = s1.charAt(counter);
                        this_char -= 32; 
                        System.out.print (this_char);
                        counter++; 
			// convert to upper case 
		}
		// bump the count of upper case characters. 
		// reset the flag indicating we will not be looking at a "first letter" next tloop.
	} 

	else {       //  this is not the first letter. Make sure it is lower case or a blank.
		// if this is an upper case letter 
			//  convert it to lower case. 
		// update count of lower case letters. 
	} 

	// print out the letter. 
	// if this is a blank 
		// set the flag that tells our loop to capitalize the first letter
		// 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


todd was a HUGE help last night, and made some comments about what to do. I just wanted to make sure that the first part of my while statement was correct before moving on to the rest.
 
Can you explain the differences or similarity between the code in red and the code in blue?

What happened when you tried to compile this?

Code:
import javax.swing.JOptionPane;

public class asgn3comments{
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();


while (counter < total) {

	[color=red]this_char = sentence.charAt(counter);[/color]
	if (first_letter == true) {   // capitalize all first letter of each word 
		if (this_char >= 97 && this_char <= 122) {
                        [color=blue]this_char = s1.charAt(counter);[/color]
                        this_char -= 32; 
                        System.out.print (this_char);
                        counter++; 
			// convert to upper case 
		}
		// bump the count of upper case characters. 
		// reset the flag indicating we will not be looking at a "first letter" next tloop.
	} 

	else {       //  this is not the first letter. Make sure it is lower case or a blank.
		// if this is an upper case letter 
			//  convert it to lower case. 
		// update count of lower case letters. 
	} 

	// print out the letter without a line return. 
	// if this is a blank 
		// set the flag that tells our loop to capitalize the first letter
		// update count of blanks 

	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 was using my profs example as a guide and thats the variable they used. i meant sentence instead of s1
 
so i should just remove the second this_char then under the if statement?


a la

Code:
import javax.swing.JOptionPane;

public class asgn3comments{
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();


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; 
                        System.out.print (this_char);
                        counter++; 
			// convert to upper case 
		}
		// bump the count of upper case characters. 
		// reset the flag indicating we will not be looking at a "first letter" next tloop.
	} 

	else {       //  this is not the first letter. Make sure it is lower case or a blank.
		// if this is an upper case letter 
			//  convert it to lower case. 
		// update count of lower case letters. 
	} 

	// print out the letter. 
	// if this is a blank 
		// set the flag that tells our loop to capitalize the first letter
		// 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
 
[code[
if (this_char >= 97 && this_char <= 122) {

this_char -= 32;
[/code]

Please, we are in the 21st century.

Try this sentence as input, just for fun (copied from Wikipedia):

An ångström or angstrom (symbol Å) (IPA pronunciation: [ˈæŋstrəm] or [ˈɔŋstrəm]; Swedish: [ˈɔ̀ŋstrœm]) is a non-SI unit of length that is internationally recognized, equal to 0.1 nanometre (nm).

Java gives you all the tools that you need to handle this, with code that is actually a lot easier than yours.
 
Please, we are in the 21st century.

Try this sentence as input, just for fun (copied from Wikipedia):



Java gives you all the tools that you need to handle this, with code that is actually a lot easier than yours.

This is the way my professor wants.
 
so i need to rest the flag indicating we wont be looking for the first letter during the next loop

do i do that by setting first_letter to false?
 
Code:
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); 
                        counter++; //bump the count of upper case characters
                            if (first_letter == false)// reset the flag indicating we will not be looking at a "first letter" next loop.
		}
		
		
	}

so just like that,
and then i have to go to the else statement?
 
actually do i even need the "if" there for setting it to false?
 
Correct, you do not need the IF. You are unconditionally going to set the flag to false.

Now, go on to the ELSE, which is the logic path for test that fail the

if (first_letter == true)

test. We want all these letters to be lower case, which means they have to be converted IF they are uppercase.
 
Code:
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); 
                        counter++; //bump the count of upper case characters
                        (first_letter == false)// reset the flag indicating we will not be looking at a "first letter" next loop.
		}
		
		
	} 

	else {    //  this is not the first letter. Make sure it is lower case or a blank. 
            if (this_char >= 65 && this_char <= 122) {// if this is an upper case letter 
		this_char += 32;// convert it to lower case. 
			
		// update count of lower case letters.

like this?
 
This is the way my professor wants.

I am very sorry to hear that. I don't know if it would be a good tactical move on your side to show him this post, but I can say that he isn't doing his students any favours, and if the question of converting from lowercase to uppercase came up in a job interview for a software developer, I am quite confident that he would fail.
 
ok so now i am a little stuck as to how to update the count of lower case letters.
 
Code:
[color=red](first_letter == false)// reset the flag indicating...[/color]

When you remove these parens... you won't be done. What else are you forgetting here?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.