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

Thread: JOption Output Message

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post JOption Output Message

    I am very new to java programming and need help!

    I am having difficulty outputting results from instructions shown:

    Write a Java program using Java, that takes an input of a name in the form lastName,firstName and outputs the name in the form firstName lastName.
    Both the input and output must use the JOptionPane.
    I am using jGrasp. I think it has something to do with .indexOf(str) format to string the results of firstName lastName. Driving me crazy.... Please help!!!


    This is what I have so far:



    import javax.swing.JOptionPane;

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

    String name;
    String str1;
    String str2;
    int index;

    str1 = "First Name";
    str2 = "Last Name";

    name = JOptionPane.showInputDialog(" Last name,First name: ");

    String title = "Name Tags";
    String answer = str1 + str2;

    JOptionPane.showMessageDialog(null, answer, title, JOptionPane.INFORMATION_MESSAGE);



    }
    }
    Last edited by richW; February 15th, 2012 at 05:14 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: JOption Output Message

    Well first you should always highlight code with [code=Java]*Code here*[/code], second you never use the variable name or the names you say are required. Third, have you looked at the String API? [Hint, you know that ", " will split the last name and first name] Fourth, to clarify you should not use indexOf for this.

    String (Java Platform SE 6)

  3. The Following User Says Thank You to Tjstretch For This Useful Post:

    richW (February 15th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JOption Output Message

    I'm not to sure if I know what you mean (highlight code and info about variables)? Please elaborate! I am very new to Java as well as this forum. Sorry for any inconvenience due to my inexperience.

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JOption Output Message

    There are many ways to accomplish your requirements. I'd recommend writing down how you would accomplish this on a piece of paper. For instance: find comma, get text on either side of comma, etc...this is your algorithm. Then translate this into code. To do the translation into code, use the methods provided to you through the API. Methods such as substring, indexOf, split, etc...might be useful: String (Java Platform SE 6)

    ...and as mentioned above, please place your code between the tags Tjstretch mentioned, it helps format the code and makes it readable.

  6. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: JOption Output Message

    The reason it's outputting "First NameLast Name" is because you told it to with

    String answer = str1 + str2;

    Use the String method split(String regex)


    let the regex be ","

    Then do something like this

    String[] array = name.split(",");

    and, an array starts with index 0 so the last name will be at index 0 and first name at index 1.

    You can access the first index by

    array[0].
    Last edited by javapenguin; February 15th, 2012 at 11:32 PM. Reason: Might be spoonfeeding but the OP might not understand arrays if he's very new to Java

Similar Threads

  1. JOption to JFrame HELP...messed my program up
    By scottgilliland2003 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 20th, 2011, 08:18 PM
  2. Hello Message
    By shaik42 in forum Member Introductions
    Replies: 1
    Last Post: July 3rd, 2011, 10:02 AM
  3. [SOLVED] I have a problem with this Joption programme.
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 12th, 2011, 03:31 AM
  4. Hello message
    By ceejaysoft in forum Member Introductions
    Replies: 1
    Last Post: March 17th, 2011, 04:18 AM
  5. JOption Pane help
    By dallas1125 in forum AWT / Java Swing
    Replies: 5
    Last Post: November 18th, 2009, 05:08 PM