File tree Expand file tree Collapse file tree 1 file changed +73
-0
lines changed
src/com/sbiswas001/twelveproject Expand file tree Collapse file tree 1 file changed +73
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .sbiswas001 .twelveproject ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .Scanner ;
5+
6+ /**
7+ * This class enters a name from user and
8+ * prints out the initials.
9+ * @author Sayan Biswas
10+ * @version 23.04.2022
11+ */
12+ public class Initials {
13+
14+ /**
15+ * Stores name
16+ */
17+ private String name ;
18+
19+ /**
20+ * Stores initials
21+ */
22+ private final ArrayList <String > initials ;
23+
24+ /**
25+ * Initialises instance variables
26+ */
27+ private Initials () {
28+ name = "" ;
29+ initials = new ArrayList <>();
30+ }
31+
32+ /**
33+ * Inputs name from user
34+ */
35+ private void input () {
36+ Scanner sc = new Scanner (System .in );
37+ System .out .print ("Enter your full name: " );
38+ name = " " + sc .nextLine ().trim ();
39+ }
40+
41+ /**
42+ * Sets the initials of the name
43+ */
44+ private void setInitials () {
45+ for (int i = 0 ; i < name .lastIndexOf (' ' ); i ++) {
46+ if (name .charAt (i ) == ' ' ) {
47+ initials .add (name .charAt (i +1 ) + ". " );
48+ }
49+ }
50+ initials .add (name .substring (name .lastIndexOf (' ' )));
51+ }
52+
53+ /**
54+ * Displays the initials
55+ */
56+ private void display () {
57+ System .out .print ("Initials: " );
58+ for (int i = 0 ; i < initials .size (); i ++) {
59+ System .out .print (initials .get (i ));
60+ }
61+ }
62+
63+ /**
64+ * Calls other methods
65+ * @param args Arguments passed to main method
66+ */
67+ public static void main (String [] args ) {
68+ Initials ob = new Initials ();
69+ ob .input ();
70+ ob .setInitials ();
71+ ob .display ();
72+ }
73+ }
You can’t perform that action at this time.
0 commit comments