This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help with curly braces!!  RSS feed

 
catherine matthews
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my code to find the correponding curly braces.....


i get the ouput as i wanted.... but when i try to implemet the same into an
input file where i tokenize every word and store them in an array and run them to find the correponding positions of the curly braces i am not getting the result i wanted...


The output that i am getting is as following
Method 118 - start at 2 and ends at 0
Method 118 - start at 0 and ends at 0
Method 118 - start at 0 and ends at 0
Method 118 - start at 0 and ends at 0
Method 118 - start at 0 and ends at 0

please tell me where i went wrong???
 
Ed Ward
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Must be with your input data.
I managed to piece together your code sample filling in the parts you left out and I get the following when the data theBraces() operates on is properly formatted.

C:\Temp\misc>java FindIndex2
the words are 0 ========String
the words are 1 ========concat()
the words are 2 ========{
the words are 3 ========verify{
the words are 4 ========No
the words are 5 ========end}
the words are 6 ========}
the words are 7 ========{
the words are 8 ========}
the words are 9 ========{
the words are 10 ========{
the words are 11 ========{
the words are 12 ========}
the words are 13 ========}
the words are 14 ========}
the words are 15 ========{
the words are 16 ========}
the words are 17 ========}
{ is here2open value is 0
{ is here3open value is 1
} is here5open value is 0 close is 0
} is here6open value is -1 close is 1
the word is now at }open value is-1
{ is here7open value is 0
} is here8open value is -1 close is 2
the word is now at }open value is-1
{ is here9open value is 0
{ is here10open value is 1
{ is here11open value is 2
} is here12open value is 1 close is 3
} is here13open value is 0 close is 4
} is here14open value is -1 close is 5
the word is now at }open value is-1
{ is here15open value is 0
} is here16open value is -1 close is 6
the word is now at }open value is-1
} is here17open value is -2 close is 7
the word is now at }open value is-1
Method 19 - start at 2 and ends at 6
Method 19 - start at 7 and ends at 8
Method 19 - start at 9 and ends at 14
Method 19 - start at 15 and ends at 16
Method 19 - start at 0 and ends at 17

So you might want to have a look at your input.

BTW, I'm sure you know your code is syntactically correct, albeit somewhat difficult to follow.
You might want to take a look atthis


As well as this

where someone politley reminds me that the use of StringTokenizer is discouraged anymore.

Additionally, while this is good practice it could be optimized a bit which would certainly help with readability.

Keep it up!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!