• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Getting wrong result from Boolean method

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code that is supposed to check if the character in a string which is '/' is at the index 4 or index 5 of the string "2/20/2020" if it is there it is true and else false. I did this for the first '/' in the string and it works perfectly fine but honestly I have no idea what is going on with the second one. If it works for the first '/' it should definitely work for the second '/'.

My code is as follows.

What's going on here?
 
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add some print statements to see what's going on.
System.out.println( geoDate.indexOf( '/' ) );
 
Master Rancher
Posts: 172
7
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the indexOf() method really doing what you think it is doing? Hint: Have a look at the method description in the java docs
 
Steven Villarreal
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GOT IT! I after looking at the API I figured out that the second If-Statement was looking for the first instance of '/' so I used lastIndexOf instead of indexOf and that seemed to do the trick!
 
Carey Brown
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now... have you tested for all date variations?
2/2/2020
2/20/2020
15/2/2020
15/20/2020

What about failure cases?
/02/20/2020/
aa/bb/cccc
 
Brecht Geeraerts
Master Rancher
Posts: 172
7
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. Well done. What indexOf() does is it looks for the first occurrence of the forward slash. The inner if statement checks whether the first occurrence is at index 4 or index 5. Neither are correct hence the reason it returns false.

Using lastIndexOf() is a good solution. Another solution is to use an overloaded version of indexOf(), namely the one that takes two parameters. That one allows you to specify an index as of which the search must start.
 
Sheriff
Posts: 24632
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also consider some code which is closer in meaning to what your original post said:


 
Sheriff
Posts: 6163
163
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you find yourself writing:
Write instead:
In general,
becomes
 
grapes are vegan food pellets. Eat this tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!