Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •