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.


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

Results 1 to 2 of 2

Thread: String Manipulation.

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Thanked 0 Times in 0 Posts

    Thumbs down String Manipulation.

    Hi, my name is Mat and I'm new to the Java language. I recently started a programming class that focuses on Java. Unfortunately, I seem to be stuck on one my of my assignments . The assignment seems fairly simple but I seem to be stuck on the string manipulation. Here is the assignment:

    I need to enter a first name and then a last name, then an age from to 10 to 99. I must manipulate the strings so that I only get the first letter of the first name and the last name. However, the age that will be entered must then be reversed.
    For example, lets say I would enter. Mat Proulx 91

    the result would be: MP19


    Here's what I have so far:

    public static void main(String[] args) {
           String firstName, lastName;
            int age;
    System.out.println("Please enter your first name.");
            Scanner keyboard = new Scanner (System.in);
            firstName = keyboard.nextLine ();
    System.out.println("Please enter your last name.");
            lastName = keyboard.nextLine ();
    System.out.println("Please enter your age.");
            age = keyboard.nextInt();
            String sentence = firstName + lastName + age;
            int position = sentence. indexOf (firstName);
            System.out.println (sentence);

    If someone could lead me in the right direction, it would be appreciated. Thank you!

    I have solved this for those who are interested :

    public class Main {
        public static void main(String[] args) {
            String firstName,lastName,age;
        Scanner keyboard = new Scanner (System.in);
        System.out.println("Please enter your first name: ");
          firstName = keyboard.nextLine();
          firstName = firstName.substring(0,1);
          firstName = firstName.toUpperCase();
        System.out.println("Please enter your last name: ");
            lastName = keyboard.nextLine();
            lastName = lastName.substring(0,1);
            lastName = lastName.toUpperCase();
        System.out.println("Please enter your age: ");
            age = keyboard.nextLine();
            System.out.println("Your ID is: " +
                    (firstName + lastName) + new StringBuffer(age).reverse());          

    Last edited by Nemus; September 22nd, 2011 at 04:56 PM.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 827 Times in 770 Posts
    Blog Entries

    Default Re: String Manipulation.

    The String API explains a wealth of methods that are at your disposal to accomplish what you need.
    String (Java Platform SE 6)
    Draw out your requirements and break the problem down....do you know how to get the first letter or a String? (hint: see the link above and the charAt and length methods).
    This link might help you with the thought process of solving problems of this nature: http://www.javaprogrammingforums.com...e-posting.html

Similar Threads

  1. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM
  2. Replies: 18
    Last Post: March 2nd, 2011, 09:52 AM
  3. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 08:58 AM
  4. question on list manipulation
    By andrefaelnar in forum Java Theory & Questions
    Replies: 11
    Last Post: August 18th, 2010, 07:30 PM
  5. String manipulation help
    By Duff in forum Loops & Control Statements
    Replies: 7
    Last Post: March 19th, 2010, 04:02 AM