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 3 of 3

Thread: nextLine problems, any help appreciated

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default nextLine problems, any help appreciated

    so far for my program i have

    import java.util.*;
     
    public class CalculateChange
    {
      public static void main(String[] args)
      {
        String s1, s2;
        Scanner keyboard = new Scanner(System.in);
     
        System.out.print("Enter your name: ");
        s1 = keyboard.nextLine();
     
        System.out.print("Enter current date: "); // in format of mm/dd/yyyy
        s2 = keyboard.nextLine();
     
        String mutation1, mutation2, mutation3;
        mutation1 = s1;
        mutation2 = mutation1.toUpperCase();
        mutation3 = mutation2.substring(0, 1);
     
        System.out.println("===== Ref. # " + mutation3);

    i am stuck from here... for the last line it is to print in the format
    of ===== Ref. #JSMITH091003 =====
    Where the J is the first letter of the name entered for s1 and SMITH is the last
    name entered for s1 and then 091003 for the date in format of yy/mm/dd
    Note that JSMITH is not what hte user entered just an example of the output and i cant use the Next( ) method


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: nextLine problems, any help appreciated

    mmm... extracting first and last names from a string. It looks like you were able to extract the first letter of the first name. Now, I'm assuming that the last name is only one word, and is separated from the first name by a space. Then, to find the last name we just need the last space character, and take everything after it as the last name.

    Ex:
    John Elwood Smith
                     ^
    Last space character is here, everything after is the last name
    There are numerous ways to do this, but the easiest is to look at every character starting from the back of the string until you've found a space, then that's the last space character.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: nextLine problems, any help appreciated

    This is what i thought. i cant seem to remember how to do this, if you could explain a bit more, thnx much

Similar Threads

  1. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 09:33 PM
  2. Need a little help. would be greatly appreciated
    By ryan29121 in forum Java Theory & Questions
    Replies: 4
    Last Post: September 27th, 2009, 02:03 PM
  3. Problems with recursion
    By KingLane in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 20th, 2009, 11:02 PM
  4. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 10:49 AM
  5. problem with Scanner nextLine() method
    By mia_tech in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 10th, 2009, 04:14 AM