Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Oct 2010
    Posts
    275
    Thanks
    10
    Thanked 1 Time in 1 Post

    Using StringBuilder

    I'm trying to figure out how to input a person's first and last name and then ask the user to change their last name using StringBuilder. Can anyone help me?

    import java.util.Scanner;
    public class Strings {


    public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    System.out.println("What is your Name: ");

    StringBuilder name = new StringBuilder();



    }

    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I wouldn't suggest doing that. If you do, you will need to manually separate the built string from the stringbuilder (the space in particular) to break the first and last name apart. So what you would do is pull the indexOf the space, and then issue a delete giving it the location of the space index (from indexOf) followed by the length of the entire string builder.

    Or you can just keep it in two array indexes instead. A String[2] is sufficient for the size required for first and last name. I don't see a reason to use a string builder here if there is intent to change one of the strings.

  • #3
    Regular Coder Taro's Avatar
    Join Date
    Oct 2011
    Location
    Geraldton, Ontario
    Posts
    155
    Thanks
    1
    Thanked 1 Time in 1 Post
    Hello,

    Why not just use a dialogue within the main method?
    Code:
    String type = JOptionPane.showInputDialog
    That way you can prompt to return a sting value, and then use that value through a transfer system; if a=b, then b=c. Just know that the end result is the cascading of the return value.

    Just remember that it is important that they may have more than one input type.
    Element ID

    Webs Support Helper

    Your friendly neighborhood Taroman.

  • #4
    Regular Coder
    Join Date
    Oct 2010
    Posts
    275
    Thanks
    10
    Thanked 1 Time in 1 Post
    I am trying to store a user inputed first and last name into a Stringbuilder. It has to be this specific way

    I wouldn't suggest doing that. If you do, you will need to manually separate the built string from the stringbuilder (the space in particular) to break the first and last name apart.
    I can put the first name and the last name in two seperate Stringbuilders if thats easier.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Why not just store them in a string array? Use a string builder if you want when you're ready to combine them.
    Mm, looks like you may just want to ask for the one name though yeah? in which case you already have a string for the entirety. There's a dozen ways to do this both with and without the string builder, but most of it still stands. If you work directly on the string input instead of worrying about a stringbuilder, you can use split on a regex to give you a string array, or you can use the indexOf for the spaces and substring to pull it apart and separate the strings.
    Stringbuilder will work pretty much the same way, target where the space is with the .indexOf(" ") check, and then use delete/append, substring or replace to just do parts of the string.


  •  

    Posting Permissions

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