| | |
Text modifier program
Thread Solved |
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!)
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!)
Java Syntax (Toggle Plain Text)
import java.util.*; public class CH10Q3 { public static void main(String[] args) { String input; Scanner keyboard = new Scanner(System.in); System.out.println("Please enter a couple sentences"); input = keyboard.nextLine(); int periods=0; while(periods!=-1) { System.out.println("While Start"); periods = input.indexOf('.', periods+3); System.out.println(periods); if(periods!=-1) Character.toUpperCase(input.charAt(periods)); System.out.println("While End"); } System.out.println(input); } }
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!
Any suggestions would be lovely.
Java Syntax (Toggle Plain Text)
import java.util.*; public class CH10Q3 { public static void main(String[] args) { String input; Scanner keyboard = new Scanner(System.in); System.out.println("Please enter a couple sentences"); input = keyboard.nextLine(); StringBuffer input2 = new StringBuffer(input); int periods=0; while(periods!=-1) { System.out.println("While Start"); periods = input2.indexOf(".", periods+2); System.out.println(periods); if(periods!=-1) { Character.toUpperCase(input2.charAt(periods)); System.out.println(Character.toUpperCase(input2.charAt(periods+2))); } System.out.println("While End"); } System.out.println(input2); } }
Any suggestions would be lovely.
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
(This post is like my "I did it WOOO HOOOOO" )
First problem was I was trying to modify a immutable object.
Second was I was going waaay out of range.
Heres the finaly code
Java Syntax (Toggle Plain Text)
import java.util.*; public class CH10Q3 { public static void main(String[] args) { String input; Scanner keyboard = new Scanner(System.in); System.out.println("Please enter a couple sentences"); input = keyboard.nextLine(); StringBuffer input2 = new StringBuffer(input); int periods=0; input2.setCharAt(0, Character.toUpperCase(input2.charAt(0))); while(periods!=-1) { periods = input2.indexOf(".", periods+2); if((periods!=-1)&&(periods<(input.length()-2))) { input2.setCharAt(periods+2, Character.toUpperCase(input2.charAt(periods+2))); } } System.out.println(input2); } }
(This post is like my "I did it WOOO HOOOOO" )
That is what often happens if you not sitting on your back-side and waiting for others to solve your issue...
Nicely done
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- import csv data line by line with ASP (ASP)
- Save data class (C++)
- Joypad to Text interface... How? (C++)
Other Threads in the Java Forum
- Previous Thread: Storing things as a byte
- Next Thread: Modifying a file.
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui health html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list login loops mac main map method methods mobile netbeans notdisplaying number online printf problem program project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






