+ 2

Can we run loop only one time so how can done it??

https://code.sololearn.com/cxrlUgxHVJkb/?ref=app In this code i want to run loop only 1 time so how can i do this Arun Tomar

23rd May 2018, 5:08 PM
Harsh Agrawal
Harsh Agrawal - avatar
13 ответов
+ 3
@Harsh Agrawal Here you go. This should help explain it to you and it also addresses all of the potential concerns that I could think of at the moment. Let me know if you have any questions. https://code.sololearn.com/cDC3h7KF6oSp/#java import java.io.*; class abc { public static void main(String args[]) throws IOException { // Receive input via System.in BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // Receive input from user and remove spaces from beginning/end of string System.out.println("Enter Your Name: "); String myName = br.readLine().trim(); // Find last period, shift forward one space, store remaining string to get last name // Trim the string to prevent unwanted spacing String lastName = myName.substring(myName.lastIndexOf(".") + 1).trim(); // Starting from beginning (0), store all text until you reach the last period // Trim the string to prevent unwated spacing String initials = myName.substring(0, myName.lastIndexOf(".") + 1).trim(); // Displayed our stored data. System.out.println("Last Name: " + lastName); System.out.println("Starting Initials: " + initials); System.out.println("Full Name: " + initials + " " + lastName); } } ----------------------------- :::: INPUT :::: J. R. R. Tolkien :::: OUTPUT :::: Last Name: Tolkien Starting Initials: J. R. R. Full Name: J. R. R. Tolkien -------------------------------- :::: INPUT :::: J.R.R. Tolkien :::: OUTPUT :::: Last Name: Tolkien Starting Initials: J.R.R. Full Name: J.R.R. Tolkien -------------------------------- :::: INPUT :::: J.R.R.Tolkien :::: OUTPUT :::: Last Name: Tolkien Starting Initials: J.R.R. Full Name: J.R.R. Tolkien ------------------------ ^As you can see, doesn't matter what you do with spaces now, nor how many starting initials either, or if there is space between it and last.
24th May 2018, 1:19 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
https://code.sololearn.com/cWb89uJON3Yj/?ref=app Harsh Agrawal thanks you the wait i change according to your program
23rd May 2018, 5:43 PM
Arun Tomar
Arun Tomar - avatar
+ 2
If you only want to loop through the code segment once, why not just remove the loop altogether? No point in a loop if you've no plans to actually loop.
23rd May 2018, 5:18 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
Just so I'm making sure I understand correctly, you want it to remove the first two initials and ONLY print the last name?
23rd May 2018, 5:22 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
Here you go. Let me know if this is what you wanted. https://code.sololearn.com/cDC3h7KF6oSp/#java import java.io.*; class abc { public static void main(String args[]) throws IOException { BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Your Name: "); String myName = br.readLine().trim(); String lastName = myName.substring(myName.indexOf(" ") + 1); String initials = myName.substring(0, myName.indexOf(" ")); System.out.println("Last Name: " + lastName); System.out.println("Starting Initials: " + initials); System.out.println("Full Name: " + initials + " " + lastName); } } ::: INPUT ::: s.k.s. chaturvedi ::: OUTPUT ::: Last Name: chaturvedi Starting Initials: s.k.s. Full Name: s.k.s. chaturvedi ::: INPUT ::: b.r. ambedkar ::: OUTPUT ::: Last Name: ambedkar Starting Initials: b.r. Full Name: b.r. ambedkar
23rd May 2018, 5:31 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
Also, on the code I posted, I would recommend doing this: String myName = br.readLine().trim(); ^The .trim() method will remove any unwanted spaces at the beginning and end of the string. That'll help prevent someone from accidentally putting spaces at the beginning or end, which could cause undesired results.
23rd May 2018, 5:37 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
Fata1 Err0r n Arun Tomar can u xplain me this program by using comments so that i can understand it properly what u do in program n if i dont do space between name n last name then it wll nt work
24th May 2018, 5:51 AM
Harsh Agrawal
Harsh Agrawal - avatar
+ 2
thnx Fata1 Err0r u r just amazing u just do without looping amazing yrr!!! i just appreciate u n thanks again n plzz explain me 15 line with fully defined each keyword,each variable n each methods u use!!! Is lastIndexOf is a method or just IndexOf is a method
24th May 2018, 3:12 PM
Harsh Agrawal
Harsh Agrawal - avatar
+ 1
Fata1 Err0r can u do it in my code so i can understand properly ya i only print the last name of whatever i type in input else it is two point or 3 it wll print only last name
23rd May 2018, 5:23 PM
Harsh Agrawal
Harsh Agrawal - avatar
+ 1
@Harsh Agrawal You're more than welcome, bro! Line 15: String lastName = myName.substring(myName.lastIndexOf(".") + 1).trim(); String <-- our variable data type lastName <--- our variable name myName <--- String that has our user input .substring() ^This is a String class method that allows you to subtract portions of a string. We do this to isolate certain portions and store it into a new variable so we can work with it without altering the original data. Syntax: substring(beginningIndex, endingIndex); ^The second argument is optional and if left blank it'll continue from its starting point to the very end. myName.lastIndexOf(".") + 1 ^We use this as our 'beginningIndex' for the substring method. lastIndexOf is a method that will find the last occurrence of whatever you specify. In this case, we're looking for the last occurrence of the periods (".") and then we do "+ 1" to get the next character after the last period. This makes it so our starting index is the first character after the last period and then we obtain the rest of the string from that point, which is how we isolate the last name from the initials. .trim() ^This gets rid of any extra whitespaces at the BEGINNING or END of the string. It does NOT get rid of spaces in between words or letters. LINE 19: myName.substring(0, myName.lastIndexOf(".") + 1).trim(); ^Similar to line 15, the difference here is that we use 0 for our substring's beginningIndex and then we use lastIndexOf(".") + 1 to end our substring after the last occurrence of a period. This allows us to get the portion of the name that is their initials and leaves out the last name.
24th May 2018, 3:33 PM
Fata1 Err0r
Fata1 Err0r - avatar
0
Change the condition to b<1
23rd May 2018, 5:10 PM
G.S.N.V. Suraj
G.S.N.V. Suraj - avatar
0
G.S.N.V. Suraj but if i do this den according to my code when i put input as like dis b.r. ambedkar so it only print ambedkar n when i put input as s.k.s. chaturvedi then it only print chaturvedi n can i find t length of . point so i can use when it is 2 dot point or 3 dot point.
23rd May 2018, 5:14 PM
Harsh Agrawal
Harsh Agrawal - avatar
0
thnxxxx buddy u r great n if i putting any question in this q/a section den plzz answer it its just a request from u... thnxxx again hve a great day wish u very gud luck in ur aim
24th May 2018, 4:14 PM
Harsh Agrawal
Harsh Agrawal - avatar