Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Converting Cases Help

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Converting Cases Help

    I can't get this to work:

    Have the name (first and last) appear using the following format: all letters are lower case except for the first letter of the first and last name: (e.g., John Betterdriver). The input for driver one should be exactly as shown above, for example enter in the name: john ‘betterDRIVER’. The output should have the name appear as ‘John Betterdriver’.

    Any tips on what I need to do?


  2. #2
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Converting Cases Help

    Quote Originally Posted by Bradshjo View Post
    I can't get this to work:

    Have the name (first and last) appear using the following format: all letters are lower case except for the first letter of the first and last name: (e.g., John Betterdriver). The input for driver one should be exactly as shown above, for example enter in the name: john ‘betterDRIVER’. The output should have the name appear as ‘John Betterdriver’.

    Any tips on what I need to do?
    Sounds like an introductory programming question, so I'll lead you to it rather than just solve it for you.

    First create a string and assign the value. String name="john betterDRIVER". Assuming that you're using an IDE like BlueJay, NetBeans or Eclipse,type -> name.

    This will bring up a list of all the methods available to you. You'll notice that there is a toLowerCase() method.

    That will be very handy to you.

    Next, you'll 2 more strings. One for the first letter, and one for everything that follows.

    String firstLetter, theRest;

    Use the substring to chop off the first character and put it into the firstLetter. Then use substring to chop off the rest of the string and put it into theRest.

    Use toUpperCase() to change the firstLetter. Now add the two strings together.

    For instance you could output them with System.out.println(firstLetter + theRest);

    This is how you would capitalize each word, but you'll have to do it for all of them. So, you'll need to have a loop. For that, look at .indexOf(). You'll see that you can "find" the first occurrence of a string within a string. So, look for the string containing a space in your original name string. This will let you find the spaces between each word.

    There are other really slick ways of doing it, but you wouldn't learn anything from them, and they would be hard to understand.

    Post your results here so others can learn from what you've done!

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Converting Cases Help

    How do you use substring to chop of the rest? I got this so far:

    System.out.println("Please enter the Driver's first name: ");
    String fName = scan.next();
    fName = Character.toUpperCase(fName.charAt(0)) + fName.substring(1);
    fRest = ???

  4. #4
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Converting Cases Help

    First put the whole string toLower() then just toUpper() the first letter, and the letter after a space.
    String name = "brett tooMEY";
    name = name.toLowerCase();
    name = name.toUpperCase(name.charAt(0)) + name.substring(1, name.indexOf(" ")) + name.toUpperCase(name.charAt(name.indexOf(" ") + 1)) + name.substring(name.indexOf(" ") + 2);
    I'm not sure if that works %100 right, but it's a basic idea.

  5. The Following User Says Thank You to Brt93yoda For This Useful Post:

    Bradshjo (September 22nd, 2010)

  6. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Converting Cases Help

    Use Substring() twice.
    First character = Substring(0,1);
    theRest= Substring (1,name.Length()-2);

    I hope you prefer the teaching approach rather than the handed to you in a single line of code.

  7. #6
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Converting Cases Help

    Bradshjo, If you have any questions about the methods I used feel free to ask me, or look at the Java String API doc here.

    Scott, whenever someone gives me an answer I usually mess around with the code to see exactly how it works. I also check the Java documents on all the methods. I made the mistake and assumed that he would do the same. I'm sorry if my post offended you in any way.
    Last edited by Brt93yoda; September 22nd, 2010 at 04:05 PM.

  8. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Converting Cases Help

    I appreciate the apology but not needed. No offense is taken. Different people have different learning methods. If he doesn't dissect your answer or follow my advice he has no hope of making it through a programming course either way. My apologies in return as re-reading my post it sounds pretty snappy, which wasn't my intention.

Similar Threads

  1. Converting Hex to Decimal
    By r2ro_serolf in forum Java Theory & Questions
    Replies: 10
    Last Post: September 4th, 2011, 05:29 PM
  2. Converting to String
    By darek9576 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 13th, 2010, 06:09 PM
  3. Converting an array
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2010, 09:58 AM
  4. [SOLVED] Help Converting to an Acronym
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 2nd, 2010, 01:37 PM
  5. Replies: 4
    Last Post: May 1st, 2009, 04:32 PM