String tokenizer inexplicable behaviour - stringtokenizer

I have started this Text Summarization and Simplification project and I am using tokenizer to get the word count to calculate the readability of the text.
Here's the code snippet:
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
wordcount++;
}
System.out.println(wordcount);
When I remove this line: System.out.println(st.nextToken()); the word count is not getting printed. The running doesn't stop. With the line the output as preferred is obtained.
Is there an explanation?

st.hasMoreTokens() does not take an element off the Enumeration. Therefore, st.hasMoreTokens() always returns true. Thus, the System.out.println() method never gets called.

Related

Getting different statement coverage for the same piece of code using gcov and gcovr

I am new to using gcov and gcovr and I wanted to get the statement coverage of a given function. It is coded in C, compiled with minGW and called from Matlab (which I use to later process the coverage information given by gcov).
I am executing the code in two different ways: for the first one I am using Simulink, in which the function inputs are given by the outputs of other functions that encompass the dynamic process I modelled on Simulink. For the second one, I am using the editor on Matlab and defining directly the inputs to the function.
Because the Simulink - executed code depends on secondary functions whose output I cannot control (contrary to the second way), I expected the statement coverage of the first execution to be worse than the second but to have the same number of statement lines (since it is exactly the same code). However, I found that:
For some function callers inside the function, the second method counts the few lines of the caller (like the first line and the following lines when the input and output variables are too long to fit in a single line), adding up statements that in reality don't exist.
The first method doesn't take into account some variable definitions at the beginning of the code, not counting them as line statements (for instance, setting input variables to 0).
Has anybody also encountered this discrepancy when getting the statement coverage of the same function? Do you know why this may be?
Thank you very much in advance!

Output on the console/Command window is always in String form/type?

I know that when we print a string on the output window using print functions in all the languages it get printed as a string only. But I want to know if I print an integer/float using same print function, is it going to get casted into string and then printed on the output screen or its type behavior is same as that of when we passed into print function?
I'm asking this question because I participated in some competitive programming and found that if we have to print integers from 1 to 500 on separate lines and if we run a "for" loop 500 times and call print() function each and every iteration/cycle then its going to take a lot of time. But if I create a variable which will hold a string only and I add the integers at the end of the string with a new line character with each and every iteration/cycle and print that string at last after trimming it then its going to take comparatively less time. Giving examples below---
This below Java "for" loop prints number during each iteration/cycle.
for(int i=1;i<=500;i++){
System.out.println(i);
}
This below Java code defines a string variable then adds numbers at the end during each cycle of the loop with new line character. At last prints the string variable by trimming it so that the last new line charcater will be removed.
String buffer_str = "";
for(int i=1;i<=500;i++){
buffer_str+=i+"\n";
}
System.out.println(buffer_str.trim());
So the question came into mind when I was thinking if both the answers are same or not. First code block prints integers while second code block prints a string.
I checked by using both code in python language too and found that the second code block is too fast while first code block was a bit slow. So second way seems good but is it appropriate and error proof?
I appreciate your answers.
Firstly, the function System.out.println converts the parameter to a string via the .toString() method (see https://docs.oracle.com/javase/tutorial/essential/io/formatting.html).
Secondly, it might be faster to first accumulate all the output into a buffer, because these I/O operations are relatively slow compared to modifying a variable in the main memory. As to why that is, consider this thread. With the second approach, you only have to go through that procedure once.

Special mark on wrapping a content using XSL-FO

I am using XSLT to transform my XML to FO output. For a particular element, I am using the attribute wrap-option set as wrap, for it to wrap in the output if it exceeds the line limit. It gets wrapped properly in the output.
But, I would like to have an additional feature that, if the text is wrapped in the output, it should have some indication to the user on the wrapping. ie, if a particular line is wrapped to next line, it should have a "+" symbol in the end of the line wherever it is wrapped.
Sample input:
Testing the wrapped input specification for understanding the wrapping has happened.
Normal line without wrapping.
Again a lengthy line which exceeds the line limit.
Current output:
Testing the wrapped input specification
for understanding the wrapping has happened.
Normal line without wrapping.
Again a lengthy line which exceeds
the line limit.
Required output:
Testing the wrapped input specification+
for understanding the wrapping has happened.
Normal line without wrapping.
Again a lengthy line which exceeds+
the line limit.
How can I achieve this result?
If you are use AH Formatter, you can use the axf:line-continued-mark extension (https://www.antennahouse.com/product/ahf64/ahf-ext.html#line-continued-mark).
There's sample FO and PDF demonstrating how to use axf:line-continued-mark available in the 'Comprehensive XSL-FO Tutorials and Samples Collection' at https://www.antennahouse.com/antenna1/comprehensive-xsl-fo-tutorials-and-samples-collection/.

Why does string.find return the indexes of the entire pattern, not the first capture?

I was wondering about the behavior of this code:
str = "abcd"
print( str:find"a(bc)d" ) -- prints 1 4 bc
print( str:find"(ab)cd" ) -- prints 1 4 ab
Even though both of the two lines are looking for, and return, different strings, they return the same indices because they have the same frame of reference. In other words, the captures are ignored when calculating the indices, but then they are returned normally.
My original question was going to be about what went wrong, but then I saw that the manual actually indicates that this is proper behavior (though it isn't very clear).
The problem was that I was trying to find something based on a marker near it, without returning the position of that marker. I expected string.find to return the position of the first capture, if there was one, so I just wrapped the part I wanted the position of with parenthesis. Obviously, that didn't help. I found a different (and better) solution to the problem, but I don't think that is always possible or convenient.
Is there any reason for string.find to behave this way? Is there any particular benefit for users? If you have absolute mastery of Lua: is there actually no case where this causes a serious problem?
Captures are a byproduct of matching. Even when you give a patten that has captures, you are still interested in matching the whole pattern. In other words, matching answers the question: where in the given string does this subtext appear? Captures are just extra bits of information about the match.
string.find returns the location of the match to allow you (for instance) to continue parsing the string after the match, possibly with a different pattern.

RunOnce in Foreach

I'm writing a little scripting language just for a bit of fun and the learning of the codes :P
I would just like your opinions/suggestions. I have an idea but I don't want to include something that people are going to want to spit on. I plan on making this language open source once, soon.
Does anybody think that it would be cool to have something like:
[Foreach] Uppercase Letter s
in Case-Insensitive Word SallySawtheSeafiShandateit:
Count++.
s.Highlight: True.
RunOnce.ProtectedMethod.ActivateProtectedMethod: IsTrue.
[Protected Method.LockTo: [Foreach]].IsTrue
StatusBar.Message: Match for s was found. Total: Count..
RunOnce.ProtectedMethod.Disable.
Explanation: What the above actually does is it searches through a string of text "SallySawtheSeafiShandateit" and highlights every single match. But when it finds the very first match for "s", it runs a method called "IsTrue", and sets the statusbar text to "match was found...". And then deactivates the RunOnce method so it may no longer be accessed, since there's no need for it to be run again.
This might not be the best example, but I think you get the idea. There have been plenty of times where I've needed to do something only once in a foreach loop, but couldn't, without writing a whole bunch of other code.
I figure, atleast this way, everything can be done in just two methods.
Please be brutally honest. :)
Thank you
This just seems like an over-complication of the following structure (in java style):
boolean ranOnce = false;
for (char c : string.toCharArray()) {
if (c != 's') continue;
if (!ranOnce) {
// do stuff once
ranOnce = true;
}
// do other stuff
}
It just seems like extreme over-engineering to me, when a single boolean and an if condition do the trick.
Hm. For this sort of situation I'd normally just use a flag variable and a conditional.
I'd reconsider "runOnce" -- it's a little ambiguous. Does it run the first iteration, the last iteration, somewhere in the middle? From what I can tell it looks like yours runs in the very first iteration, but then again what use would displaying the total count be in the first iteration? You'll know it's just "1".
For my money, I think I'd actually use two keywords that fired events/methods/etc at the first iteration and at the last iteration, respectively.

Resources