| | |
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
- Save data class (C++)
- Joypad to Text interface... How? (C++)
- import csv data line by line with ASP (ASP)
Other Threads in the Java Forum
- Previous Thread: Storing things as a byte
- Next Thread: Modifying a file.
| Thread Tools | Search this Thread |
account android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer homework html ide image inheritance integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux loop method midlethttpconnection migrate mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program property ria scanner server set sharepoint smart sms smsspam sourcelabs splash sql sqlite subclass support swing testautomation textfield threads tree trolltech unlimited utility windows






