Results 1 to 19 of 19
Like Tree1Likes
  • 1 Post By jim829

Thread: Printing a users name in different ways

  1. #1
    vex91 is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Printing a users name in different ways

    Hello,

    I am new to java and am taking a class for it. I am stuck on this assignment and cannot figure out how to get it to work I've been trying for hours. It doesn't help that my teacher has a thick accent and I can't understand what he says half the time.

    Here is what I have to do:
    Have the user input his First, middle and last name and... (Full name must be one string)

    - Print just the initials in upper case letters
    - Print the last name in upper case followed by a comma and the first name in lower case with the
    first letter capitalized and the middle initial capitalized followed by a period
    - Print the last name comma first name and middle name – all names with first letter capitalized.

    Now normally I would be able to figure this out, but what throws me of is that we have to check to see if a middle name has been inputted and if not then run without it. Also, we are NOT allowed to use arrays.

    ************************************************** ****
    SAMPLE OF WHAT IT SHOULD OUTPUT:

    What are your first, middle, and last names?
    david john smith
    Your initials are: DJS
    Variation one: SMITH, David J.
    Variation two: Smith, David John

    What are your first, middle and last names?
    Faye Navabi
    Your initials are: FN
    Variation One: NAVABI, Faye
    Variation Two: Navabi, Faye

    ************************************************** ***

    Please, any help would be appreciated I am stuck and it is getting very stressful not knowing what to do.
    Thanks

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Printing a users name in different ways

    What have you done so far? We cannot help if we cannot see your code. You also need to ask a specific question. "I do not know what to do" is not specific and not a question.

    If you reply "I don't know where to start" then you should quit. Do you know what a class is? What a method is? How to declare variables? How to assign values to variables? How to get user input? If you answered yes to any of those questions then you do know where to start.

  3. #3
    vex91 is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: Printing a users name in different ways

    Quote Originally Posted by Junky View Post
    What have you done so far? We cannot help if we cannot see your code. You also need to ask a specific question. "I do not know what to do" is not specific and not a question.

    If you reply "I don't know where to start" then you should quit. Do you know what a class is? What a method is? How to declare variables? How to assign values to variables? How to get user input? If you answered yes to any of those questions then you do know where to start.
    Oh sorry, heres what I have so far.. I had more but I did not include it because I'm pretty sure it is wrong:

    import java.util.Scanner;

    public class Assignment3 {

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("What are your first, middle, and last names?");
    String fullName = in.nextLine();

    String firstName = fullName.substring(0, fullName.indexOf(" "));
    String middleName = fullName.substring(firstName.length()+1,
    fullName.lastIndexOf(" "));
    String lastName = fullName.substring(middleName.length()+firstName.l ength()+2,
    fullName.length());


    System.out.println("Your initials are " + firstName.charAt(0) + middleName.substring(middleName.length()-1));

    }

    }

    I guess I am basically asking how to make a loop or an if else statement that would check to see if the person has included their last name, and if not, run the program anyway. Because as of right now, when a person does not include their middle name it comes up as an error. Thanks
    Last edited by vex91; 09-20-2013 at 04:10 AM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Printing a users name in different ways

    The String class has a method called split that would be very useful.

  5. #5
    vex91 is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: Printing a users name in different ways

    I understand how to split the string into three parts and I have done that successfully in my previous code, but I again ran into a problem when the user only inputs their first and last name, and does not include their middle name.
    Last edited by vex91; 09-20-2013 at 04:17 AM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: Printing a users name in different ways

    The OP can't use that. Arrays are not allowed!

    However, the Scanner class has all that is needed.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: Printing a users name in different ways

    Well, if they only enter their first and middle names, what does that make the middle name?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    vex91 is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: Printing a users name in different ways

    Quote Originally Posted by jim829 View Post
    Well, if they only enter their first and middle names, what does that make the middle name?

    Regards,
    Jim
    I meant if they only input their first and last name. How can I got about doing this? I am so frustratingly confused. And at this point I am going to use arrays
    Last edited by vex91; 09-20-2013 at 04:23 AM.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: Printing a users name in different ways

    You must make an assumption about the input. If they put in just two strings separated by a space vs three strings separated by spaces. Think about it for a second. If they only enter two strings, what does that make the last String, a middle name or a last name?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    vex91 is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: Printing a users name in different ways

    Quote Originally Posted by jim829 View Post
    You must make an assumption about the input. If they put in just two strings separated by a space vs three strings separated by spaces. Think about it for a second. If they only enter two strings, what does that make the last String, a middle name or a last name?

    Regards,
    Jim
    I understand if they only input two strings that it is first and last name, I do. What I don't understand is how to get the program to run when it is expecting to turn 3 string into intials and it only receives 2.

    Here is my code now



    import java.util.Scanner;

    public class Assignment3 {

    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    System.out.print("Please enter your first name, middle name and last name: ");
    String name = scan.nextLine();

    String[] nameParts = name.split(" ");

    char firstInitial = nameParts[0].charAt(0);
    char middleInitial = nameParts[1].charAt(0);
    char lastInitial = nameParts[2].charAt(0);

    System.out.println ("Initials Are " + firstInitial + middleInitial + lastInitial);

    }
    }


    it works perfectly fine with taking the 3 strings and making them into 3 initials ( Bob Michael Kane = BMK ) But when there is no middle name inputted then it comes up as an error. If the user inputs "Bob Kane" I need it to output "BK" and I don't understand how to do so.
    Last edited by vex91; 09-20-2013 at 04:50 AM.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: Printing a users name in different ways

    First, you said you could not use arrays! So can you or can't you? They are not really needed.

    In any event, you need to focus on simply reading in the names correctly. Forget about doing anything with initials. Just get the first, middle and last names or first and last names. Then you can focus on the rest of the stuff.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    vex91 is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: Printing a users name in different ways

    Yeah I read the assignment wrong I AM allowed to use arrays apparently, anywho this is what my code consists of currently:

    -----------------------------------------------------------------------------------
    import java.util.Scanner;

    public class Assignment3 {

    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter your first name, middle name and last name: ");
    String name = scan.nextLine();

    String[] fullName = name.split(" ");

    String firstName = fullName[0];
    String middleName = fullName[1];
    String lastName = fullName[2];

    char firstInitial = fullName[0].charAt(0);
    char middleInitial = fullName[1].charAt(0);
    char lastInitial = fullName[2].charAt(0);


    System.out.println("Your initials Are " + Character.toUpperCase(firstInitial) + Character.toUpperCase(middleInitial) + Character.toUpperCase(lastInitial));

    }
    }

    ---------------------------------------------------------------------------------

    I am still getting an error when the user does not include a middle name. Can you please explain a way to use a loop or if/else statement to get around this? That would be so helpful, thanks

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: Printing a users name in different ways

    Did you check the length of the returned array from split?

    Regards,
    Jim
    Tolls likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    vex91 is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: Printing a users name in different ways

    Quote Originally Posted by jim829 View Post
    Did you check the length of the returned array from split?

    Regards,
    Jim
    I'm sorry, but I don't know what that means, how would I do that?

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: Printing a users name in different ways

    Are you familiar with arrays? they have a length field which indicates their size.

    Java Code:
    String [] foo = new String[4];
    int len = foo.length; // len is now set to 4;
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  16. #16
    vex91 is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: Printing a users name in different ways

    Quote Originally Posted by jim829 View Post
    Are you familiar with arrays? they have a length field which indicates their size.

    Java Code:
    String [] foo = new String[4];
    int len = foo.length; // len is now set to 4;
    Regards,
    Jim
    Well I just emailed my professor and he said that apparently we really are NOT allowed to use arrays OR loops.... so there goes my whole code :( Now I really am stuck

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: Printing a users name in different ways

    Fear not! You don't need arrays or loops. But you do need to learn about the String class. Specifically the indexOf methods and the substring methods. You need to practice using them to do your own version of split.

    So simply write a test program to break up strings separated by a space. Note: if you are guaranteed that the names will be separated by a single space then it will be a little easier to do. Otherwise, you need to consider multiple spaces separating the parts of the name.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  18. #18
    vex91 is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: Printing a users name in different ways

    Quote Originally Posted by jim829 View Post
    Fear not! You don't need arrays or loops. But you do need to learn about the String class. Specifically the indexOf methods and the substring methods. You need to practice using them to do your own version of split.

    So simply write a test program to break up strings separated by a space. Note: if you are guaranteed that the names will be separated by a single space then it will be a little easier to do. Otherwise, you need to consider multiple spaces separating the parts of the name.

    Regards,
    Jim
    Ok, so I am starting from scratch again. I have been able to use the indexOf method to determine which part of the name is which (first, middle, last) but I am still running into trouble if the middle name is not inputted. How would I make an if/else that would check how many spaces are in the string? Because if I can do that I can have it count the amount of spaces in the inputted string and if there is 2 spaces, then there would have been a first, middle and last name inputted. And if there is only one space then there is only a first and last name inputted. How can I go about doing this?

    Thanks

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: Printing a users name in different ways

    What does indexOf return if you try and find a second space and it's not there?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. help on different ways to construct objects...
    By ajetrumpet in forum New To Java
    Replies: 18
    Last Post: 05-02-2013, 09:47 AM
  2. Replies: 0
    Last Post: 12-26-2011, 03:52 PM
  3. Need Help splitting up an Int in a variety of ways
    By JoKeR313 in forum New To Java
    Replies: 9
    Last Post: 11-10-2010, 06:22 AM
  4. Displaying characters in many ways.
    By TampaTechGuy in forum New To Java
    Replies: 7
    Last Post: 01-02-2008, 09:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •