How to append an entered Social Security number to xxx-xx-1111?How would you access Object properties from within an object method?How to round a number to n decimal places in JavaHow to append something to an array?Are getters and setters poor design? Contradictory advice seenHow to append text to an existing file in JavaHow do I append one string to another in Python?How do you append to a file in Python?Verifying if an input string is a correct Social Security NumberWhen the user types 'EXIT' the program should close and show all previous inputsMaking a getter that accesses another class in JAVA

Is there a push, in the United States, to use gender-neutral language and gender pronouns (when they are given)?

In the twin paradox does the returning twin also come back permanently length contracted flatter than the twin on earth?

This is a Noteworthy Riddle

Did Terry Pratchett ever explain the inspiration behind the Luggage?

How safe is the 4% rule if the U.S. goes back to the mean?

What is a "G.O.A.T" game?

Idiom for a situation or event that makes one poor or even poorer?

"A tin of biscuits" vs "A biscuit tin"

How do I copy an installed steam game on my PC to an external hard drive?

Fermat's polygonal number theorem

Is a list of the most common English words copyrightable?

Hero battle game

Which culture used no personal names?

Translation of: 美しいと思ってしまったのだ

Why does Principal Vagina say, "no relation" after introducing himself?

Split mile limits to the thousandth based on ID

Find number 8 with the least number of tries

Decrypting Multi-Prime RSA with e, N, and factors of N given

Code Golf Measurer © 2019

Encountering former, abusive advisor at a conference

Split telescope into two eyes

Why is Trump releasing or not of his taxes such a big deal?

Why isn't Hagrid removed from Hogwarts sooner in Harry's would-be 7th year?

Are dead worlds a good galactic barrier?



How to append an entered Social Security number to xxx-xx-1111?


How would you access Object properties from within an object method?How to round a number to n decimal places in JavaHow to append something to an array?Are getters and setters poor design? Contradictory advice seenHow to append text to an existing file in JavaHow do I append one string to another in Python?How do you append to a file in Python?Verifying if an input string is a correct Social Security NumberWhen the user types 'EXIT' the program should close and show all previous inputsMaking a getter that accesses another class in JAVA






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









-1

















I am learning Java and am working on a program for a class. I've completed the bulk of the program so not much help is needed.



How can I convert a SSN (entered by scanner) from, 111-11-1111 to xxx-xx-1111?



I also need to validate that the format of the numbers entered is correct. There is a main class and an Astronaut class where I have getters and setters for the SSN. I already have a toString override method to format it the way I want when I run the program.



@Override
public String toString()
super.toString();
System.out.println("SSN: " + ssn);
return "";










share|improve this question























  • 1





    What do you expect super.toString() to achieve?

    – PM 77-1
    Mar 28 at 21:26











  • Aside from main question: toString() shouldn't be responsible for printing anything, but generating String representation of an object which then can be used as client want, for instance it can be printed. Using that logic your super.toString() would be redundant because it would return some value which you are ignoring. If your superclass really generates values which you want to also print along with your Astronaut then you probably wanted something like String parentStr = super.toString(); ...; return parentStr+other+elements;

    – Pshemo
    Mar 28 at 21:33






  • 1





    Anyway easiest way to replacing single characters with other characters would be converting string to char[], modifying that char[], and creating new String(modifiedCharArray). You could also use substring method to get only part which you are interested in and concatenate it with mask.

    – Pshemo
    Mar 28 at 21:35











  • I forgot to mention that the Astronaut is an extension of another class

    – JavaLearner1234
    Mar 28 at 22:02

















-1

















I am learning Java and am working on a program for a class. I've completed the bulk of the program so not much help is needed.



How can I convert a SSN (entered by scanner) from, 111-11-1111 to xxx-xx-1111?



I also need to validate that the format of the numbers entered is correct. There is a main class and an Astronaut class where I have getters and setters for the SSN. I already have a toString override method to format it the way I want when I run the program.



@Override
public String toString()
super.toString();
System.out.println("SSN: " + ssn);
return "";










share|improve this question























  • 1





    What do you expect super.toString() to achieve?

    – PM 77-1
    Mar 28 at 21:26











  • Aside from main question: toString() shouldn't be responsible for printing anything, but generating String representation of an object which then can be used as client want, for instance it can be printed. Using that logic your super.toString() would be redundant because it would return some value which you are ignoring. If your superclass really generates values which you want to also print along with your Astronaut then you probably wanted something like String parentStr = super.toString(); ...; return parentStr+other+elements;

    – Pshemo
    Mar 28 at 21:33






  • 1





    Anyway easiest way to replacing single characters with other characters would be converting string to char[], modifying that char[], and creating new String(modifiedCharArray). You could also use substring method to get only part which you are interested in and concatenate it with mask.

    – Pshemo
    Mar 28 at 21:35











  • I forgot to mention that the Astronaut is an extension of another class

    – JavaLearner1234
    Mar 28 at 22:02













-1












-1








-1








I am learning Java and am working on a program for a class. I've completed the bulk of the program so not much help is needed.



How can I convert a SSN (entered by scanner) from, 111-11-1111 to xxx-xx-1111?



I also need to validate that the format of the numbers entered is correct. There is a main class and an Astronaut class where I have getters and setters for the SSN. I already have a toString override method to format it the way I want when I run the program.



@Override
public String toString()
super.toString();
System.out.println("SSN: " + ssn);
return "";










share|improve this question
















I am learning Java and am working on a program for a class. I've completed the bulk of the program so not much help is needed.



How can I convert a SSN (entered by scanner) from, 111-11-1111 to xxx-xx-1111?



I also need to validate that the format of the numbers entered is correct. There is a main class and an Astronaut class where I have getters and setters for the SSN. I already have a toString override method to format it the way I want when I run the program.



@Override
public String toString()
super.toString();
System.out.println("SSN: " + ssn);
return "";







java append tostring






share|improve this question















share|improve this question













share|improve this question




share|improve this question



share|improve this question








edited Mar 28 at 21:58









Steve

4,8351 gold badge8 silver badges31 bronze badges




4,8351 gold badge8 silver badges31 bronze badges










asked Mar 28 at 21:21









JavaLearner1234JavaLearner1234

11 bronze badge




11 bronze badge










  • 1





    What do you expect super.toString() to achieve?

    – PM 77-1
    Mar 28 at 21:26











  • Aside from main question: toString() shouldn't be responsible for printing anything, but generating String representation of an object which then can be used as client want, for instance it can be printed. Using that logic your super.toString() would be redundant because it would return some value which you are ignoring. If your superclass really generates values which you want to also print along with your Astronaut then you probably wanted something like String parentStr = super.toString(); ...; return parentStr+other+elements;

    – Pshemo
    Mar 28 at 21:33






  • 1





    Anyway easiest way to replacing single characters with other characters would be converting string to char[], modifying that char[], and creating new String(modifiedCharArray). You could also use substring method to get only part which you are interested in and concatenate it with mask.

    – Pshemo
    Mar 28 at 21:35











  • I forgot to mention that the Astronaut is an extension of another class

    – JavaLearner1234
    Mar 28 at 22:02












  • 1





    What do you expect super.toString() to achieve?

    – PM 77-1
    Mar 28 at 21:26











  • Aside from main question: toString() shouldn't be responsible for printing anything, but generating String representation of an object which then can be used as client want, for instance it can be printed. Using that logic your super.toString() would be redundant because it would return some value which you are ignoring. If your superclass really generates values which you want to also print along with your Astronaut then you probably wanted something like String parentStr = super.toString(); ...; return parentStr+other+elements;

    – Pshemo
    Mar 28 at 21:33






  • 1





    Anyway easiest way to replacing single characters with other characters would be converting string to char[], modifying that char[], and creating new String(modifiedCharArray). You could also use substring method to get only part which you are interested in and concatenate it with mask.

    – Pshemo
    Mar 28 at 21:35











  • I forgot to mention that the Astronaut is an extension of another class

    – JavaLearner1234
    Mar 28 at 22:02







1




1





What do you expect super.toString() to achieve?

– PM 77-1
Mar 28 at 21:26





What do you expect super.toString() to achieve?

– PM 77-1
Mar 28 at 21:26













Aside from main question: toString() shouldn't be responsible for printing anything, but generating String representation of an object which then can be used as client want, for instance it can be printed. Using that logic your super.toString() would be redundant because it would return some value which you are ignoring. If your superclass really generates values which you want to also print along with your Astronaut then you probably wanted something like String parentStr = super.toString(); ...; return parentStr+other+elements;

– Pshemo
Mar 28 at 21:33





Aside from main question: toString() shouldn't be responsible for printing anything, but generating String representation of an object which then can be used as client want, for instance it can be printed. Using that logic your super.toString() would be redundant because it would return some value which you are ignoring. If your superclass really generates values which you want to also print along with your Astronaut then you probably wanted something like String parentStr = super.toString(); ...; return parentStr+other+elements;

– Pshemo
Mar 28 at 21:33




1




1





Anyway easiest way to replacing single characters with other characters would be converting string to char[], modifying that char[], and creating new String(modifiedCharArray). You could also use substring method to get only part which you are interested in and concatenate it with mask.

– Pshemo
Mar 28 at 21:35





Anyway easiest way to replacing single characters with other characters would be converting string to char[], modifying that char[], and creating new String(modifiedCharArray). You could also use substring method to get only part which you are interested in and concatenate it with mask.

– Pshemo
Mar 28 at 21:35













I forgot to mention that the Astronaut is an extension of another class

– JavaLearner1234
Mar 28 at 22:02





I forgot to mention that the Astronaut is an extension of another class

– JavaLearner1234
Mar 28 at 22:02












2 Answers
2






active

oldest

votes


















1


















You can do something like this:



@Override
public String toString()
return "SSN: XXX-XX-" + ssn.substring(7);






share|improve this answer

































    0


















    In order to validate that the number of digits entered is correct, you can use a regex expression. For example: ssn.matches("(\d-)?\d3-\d2-\d4")






    share|improve this answer



























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );














      draft saved

      draft discarded
















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55407037%2fhow-to-append-an-entered-social-security-number-to-xxx-xx-1111%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown


























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1


















      You can do something like this:



      @Override
      public String toString()
      return "SSN: XXX-XX-" + ssn.substring(7);






      share|improve this answer






























        1


















        You can do something like this:



        @Override
        public String toString()
        return "SSN: XXX-XX-" + ssn.substring(7);






        share|improve this answer




























          1














          1










          1









          You can do something like this:



          @Override
          public String toString()
          return "SSN: XXX-XX-" + ssn.substring(7);






          share|improve this answer














          You can do something like this:



          @Override
          public String toString()
          return "SSN: XXX-XX-" + ssn.substring(7);







          share|improve this answer













          share|improve this answer




          share|improve this answer



          share|improve this answer










          answered Mar 28 at 21:30









          ipaveipave

          4263 silver badges12 bronze badges




          4263 silver badges12 bronze badges


























              0


















              In order to validate that the number of digits entered is correct, you can use a regex expression. For example: ssn.matches("(\d-)?\d3-\d2-\d4")






              share|improve this answer






























                0


















                In order to validate that the number of digits entered is correct, you can use a regex expression. For example: ssn.matches("(\d-)?\d3-\d2-\d4")






                share|improve this answer




























                  0














                  0










                  0









                  In order to validate that the number of digits entered is correct, you can use a regex expression. For example: ssn.matches("(\d-)?\d3-\d2-\d4")






                  share|improve this answer














                  In order to validate that the number of digits entered is correct, you can use a regex expression. For example: ssn.matches("(\d-)?\d3-\d2-\d4")







                  share|improve this answer













                  share|improve this answer




                  share|improve this answer



                  share|improve this answer










                  answered Mar 28 at 21:49









                  AtroAtro

                  11 bronze badge




                  11 bronze badge































                      draft saved

                      draft discarded















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55407037%2fhow-to-append-an-entered-social-security-number-to-xxx-xx-1111%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown









                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript