Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-17-2007, 01:13 AM   PM User | #1
derajfast
New to the CF scene

 
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
derajfast is an unknown quantity at this point
Having a bit of trouble

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

Code:
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
derajfast is offline   Reply With Quote
Old 10-17-2007, 01:18 AM   PM User | #2
derajfast
New to the CF scene

 
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
derajfast is an unknown quantity at this point
also, i made comments as to what i THINK i need to do, but i am not exactly sure about how to go about them. thanks
derajfast is offline   Reply With Quote
Old 10-17-2007, 04:53 AM   PM User | #3
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
Before I give any specific answer, I'm wondering if you can use the functions in the String and Character Library? If so then you can change some of the way you are doing things to make it easier to understand.

When I say Character and String Libraries btw I mean these things in the API:

Character
String
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:00 AM.


Advertisement
Log in to turn off these ads.