+ 3

Code Coach solution issue - Java

https://code.sololearn.com/cW0kVfvYmrYw If you give two inputs either https://youtu.be/CwROcBKXsus (short youtube link) or https://www.youtube.com/watch?v=CwROcBKXsus a longer youtube link we are expected to get the output CwROcBKXsus however in my case I am not! This is a method quite based on compromise rather than a true solution to the problem so i would be grateful if someone could provide other ways of solving this as well.

18th Sep 2021, 7:09 PM
Ojas Kanotra
Ojas Kanotra - avatar
6 Answers
+ 5
Thanks everyone for your answers it helped a lot.
19th Sep 2021, 3:45 AM
Ojas Kanotra
Ojas Kanotra - avatar
+ 7
Ojas Kanotra , ChaoticDawg , has given you a hint how this task can be done in a simple way. to make it a bit more clear: ▪︎after taken the input with scanner and storing it in a string variable, you can get the length of the input string by using the string method .length() ▪︎take the length and subtract 11 (the length of the relevant part of the link) and store this value in an integer variable ▪︎use substring method with the input string and with the int argument from last step. this will give you the final link for output .... Scanner read = new Scanner(System.in); String link = read.nextLine(); int startPos = link.length() - 11; System.out.println(link.substring(startPos)); ....
18th Sep 2021, 8:48 PM
Lothar
Lothar - avatar
+ 4
You may or may not have noticed that the info that you need is always the last 11 characters of the input. This means that you can simply solve this by getting that portion (substring) of the input. This means you don't need to look for the difference between the 2 styles of input and can either just get the substring using the last index of the input for 'end' and the 11th index from the 'end' as the 'start' for your substring. You could also just loop through the last 11 characters if you prefer to use a loop.
18th Sep 2021, 8:28 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
Ojas Kanotra I did it the hard way too 🙃 I reversed the string (which I also did the hard way) and then used indexOf to find the = or / (which told me the format) and then used the return value to extract the substring. I didn’t realize it would always be 11 characters 🤣 My code was twice as long as yours 😲
19th Sep 2021, 2:27 AM
Paul K Sadler
Paul K Sadler - avatar
+ 3
At this lines: if (x.charAt(13) == c) { The c must be 'c' Same in the other case. Your loops have to stop at y < x.lenght() not at y <= x.lenght() Because: The index of chars in the string is zero based. First char is at pos. (0). The last one is at pos. (lenght - 1)
18th Sep 2021, 7:58 PM
Coding Cat
Coding Cat - avatar
+ 2
Its showing string index out of bound exception i think their is mistake is in your loop check it properly
18th Sep 2021, 7:16 PM
A S Raghuvanshi
A S Raghuvanshi - avatar