Text modifier program

Thread Solved

Join Date: May 2008
Posts: 128
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

Text modifier program

 
0
  #1
Dec 6th, 2008
Hi guys,
I'm new and I'm totally stuck on a question for class! Basically we have to get the user to enter a few sentences and then change the first letter of every sentence to a capital version. I'll post my code below.
(I'm not asking for the answer just some help would be nice!)

  1. import java.util.*;
  2. public class CH10Q3
  3. {
  4. public static void main(String[] args)
  5. {
  6. String input;
  7. Scanner keyboard = new Scanner(System.in);
  8.  
  9. System.out.println("Please enter a couple sentences");
  10. input = keyboard.nextLine();
  11. int periods=0;
  12.  
  13. while(periods!=-1)
  14. {
  15. System.out.println("While Start");
  16. periods = input.indexOf('.', periods+3);
  17. System.out.println(periods);
  18. if(periods!=-1)
  19. Character.toUpperCase(input.charAt(periods));
  20. System.out.println("While End");
  21. }
  22. System.out.println(input);
  23. }
  24. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 128
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

Re: Text modifier program

 
0
  #2
Dec 7th, 2008
Okay heres a small update, I am converting my string into a buffered string so its mutable. Here is the code I have now, only problem is it goes out of range!

  1. import java.util.*;
  2. public class CH10Q3
  3. {
  4. public static void main(String[] args)
  5. {
  6. String input;
  7. Scanner keyboard = new Scanner(System.in);
  8.  
  9. System.out.println("Please enter a couple sentences");
  10. input = keyboard.nextLine();
  11. StringBuffer input2 = new StringBuffer(input);
  12. int periods=0;
  13.  
  14. while(periods!=-1)
  15. {
  16. System.out.println("While Start");
  17. periods = input2.indexOf(".", periods+2);
  18. System.out.println(periods);
  19. if(periods!=-1)
  20. {
  21. Character.toUpperCase(input2.charAt(periods));
  22. System.out.println(Character.toUpperCase(input2.charAt(periods+2)));
  23. }
  24. System.out.println("While End");
  25. }
  26. System.out.println(input2);
  27. }
  28. }

Any suggestions would be lovely.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 128
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

Re: Text modifier program

 
0
  #3
Dec 7th, 2008
Ah well I figured it out on my own.
First problem was I was trying to modify a immutable object.
Second was I was going waaay out of range.

Heres the finaly code
  1. import java.util.*;
  2. public class CH10Q3
  3. {
  4. public static void main(String[] args)
  5. {
  6. String input;
  7. Scanner keyboard = new Scanner(System.in);
  8.  
  9. System.out.println("Please enter a couple sentences");
  10. input = keyboard.nextLine();
  11. StringBuffer input2 = new StringBuffer(input);
  12. int periods=0;
  13.  
  14. input2.setCharAt(0, Character.toUpperCase(input2.charAt(0)));
  15.  
  16. while(periods!=-1)
  17. {
  18. periods = input2.indexOf(".", periods+2);
  19. if((periods!=-1)&&(periods<(input.length()-2)))
  20. {
  21. input2.setCharAt(periods+2, Character.toUpperCase(input2.charAt(periods+2)));
  22. }
  23. }
  24. System.out.println(input2);
  25. }
  26. }

(This post is like my "I did it WOOO HOOOOO" )
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,181
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 481
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Text modifier program

 
0
  #4
Dec 7th, 2008
That is what often happens if you not sitting on your back-side and waiting for others to solve your issue...
Nicely done
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 128
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

Re: Text modifier program

 
0
  #5
Dec 7th, 2008
Haha, thanks. Well I see so many people asking for the answers, and that's not going to help me with my Comp. Sci. Degree... plus I want to be able to write nice software without having to ask people around me constantly for help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC