DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Text modifier program (http://www.daniweb.com/forums/thread161090.html)

PhiberOptik Dec 6th, 2008 11:25 pm
Text modifier program
 
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!)

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);
    }
}

PhiberOptik Dec 7th, 2008 12:09 am
Re: Text modifier program
 
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!

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.

PhiberOptik Dec 7th, 2008 12:17 am
Re: Text modifier program
 
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
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" )

peter_budo Dec 7th, 2008 4:25 am
Re: Text modifier program
 
That is what often happens if you not sitting on your back-side and waiting for others to solve your issue...
Nicely done

PhiberOptik Dec 7th, 2008 3:08 pm
Re: Text modifier program
 
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.


All times are GMT -4. The time now is 10:34 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC