• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is wrong with the code?  RSS feed

 
Dana Horst
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys, please help me with the following problem. i have a code



but i get
Birthday 22/03/1986
day22
month0
year198

Question: which indexes i have to type in to get a desired result and please explain why. Thank you

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Java API about String.substring(int, int)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

The last index is exclusive, so you need to write:
Another option you might try is to use String.split() and use "/" as a separator.
 
Dana Horst
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!
 
Jahangir Alam
Greenhorn
Posts: 15
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
substring(int beginIndex, int endIndex)

or

substring(int beginIndex).

begnIndex indicates the starting point including that number as well.

but endIndex indicates the ending point but excluding that index.

For example:

String day = "22/03/1986";
String s3 = day.substring(6,9);

The result is 198.
But you expected 1986.
It does not happen because it exclueds the index 9, I mean the "6", so you get the result:198.
Similarly,

String s2 = day.substring(3,4);

Here, the result is only "0", because it takes the starting index that is index 3 but excludes the ending index that is index 4.
 
Dana Horst
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you!
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as an FYI, you will probably get even more help if you UseAMeaningfulSubjectLine.

The topics you've created so far are:

what is wrong with the code?
understanding the code
need a hand to understand the code
please explain the output
guys, please help me understand the code

Some people may skip reading your posts because they may think they have already read it if all posts have very similar titles.
 
Dana Horst
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for comments!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!