Results 1 to 5 of 5
  1. #1
    enjoy2225 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Last Name, First Name switch Program help.

    Hello all, I am currently taking my first basic programming course and have an assignment I'm having trouble with.

    Problem: Have user input a name in the format ( " Last Name, First Name "), and display the New name as (" First Name, Last Name " ) With the first letters of both names being capitalized, and the rest of the letters being lowercase.

    My professor wants us to use a indexof method to find the ',' in the inputted name to separate the two names.
    Does anyone know how to do this? It sounds simple enough but I am completely new to this Java stuff and don't get it.

    This is how far I have gotten with the help of a classmate.

    import javax.swing.JOptionPane;

    public class NameTags

    {
    public static void main(String[] args)
    {

    String name;
    String Str1;
    String Str2;
    int index;

    Str1=JOptionPane.showInputDialog(" Last Name,First Name: ");
    name= Str1;
    name.length();
    index= Str1.indexOf(',');

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: Last Name, First Name switch Program help.

    professor wants us to use a indexof method
    Have you read the API doc for the String class's indexOf() method? That's a good place to start.

    You need to add the ending }s for the main method and class and then compile and execute the code.
    Add a call to the System.out.println(<put vars here>) to print out the values of the variables so you can see what the code has done when it was executed.
    Last edited by Norm; 10-05-2012 at 04:10 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Last Name, First Name switch Program help.

    Please go through BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    AeneasTroy is offline Arma virumque cano
    Join Date
    Oct 2012
    Location
    Indianapolis
    Posts
    20
    Rep Power
    0

    Default Re: Last Name, First Name switch Program help.

    You are probably going to want to use a string.substring() function.

    Java Code:
    import javax.swing.JOptionPane;
    
    public class NameTags
    {
      public static void main(String[] args)
      {
        
        String name = JOptionPane.showInputDialog("Last Name, First Name: ");
        //provided the user uses a comma, name will now equal something like: "Smith, Joe"
        
        //To find the comma:
        int index = name.indexOf(",");
        
        //You will need 2 substrings, and since this is homework, I will leave you to figure out how to finish it:
        //String strLastName = name.substring(0, index of the comma);
        //String strFirstName = name.substring(index of the comma, name.length());
     
        //Display the results
        
        //exit the program
        System.exit(0);
      }
    }
    In order to get this right, you want to omit the comma that the user entered, otherwise it will still be there when you display your results.
    This can be done by adjusting the index.

    Good luck

  5. #5
    enjoy2225 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Last Name, First Name switch Program help.

    Wow thank you guys so much I really appreciate it. I will try to press on and let you know how it goes!

Similar Threads

  1. Switch
    By java4amanda in forum New To Java
    Replies: 13
    Last Post: 03-21-2012, 09:53 AM
  2. Replies: 4
    Last Post: 02-13-2011, 12:03 PM
  3. switch
    By dj kourampies in forum New To Java
    Replies: 17
    Last Post: 01-30-2009, 05:32 PM
  4. switch
    By dj kourampies in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 08:46 AM
  5. Switch help please!!!!
    By soc86 in forum New To Java
    Replies: 6
    Last Post: 11-23-2008, 07:25 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
  •