Link to home
Start Free TrialLog in
Avatar of LarryAndro
LarryAndro

asked on

Beanshell Console Hangup During WHILE Loop

I am having problems with a WHILE loop in the Beanshell console; execution hangs.  I don't get an error either.  

First of all, this code works well, displaying the first line of the text file...

DataInputStream in=new DataInputStream(new FileInputStream("C:\\tmp\\tmp5.txt"));
BufferedReader d=new BufferedReader(new InputStreamReader(in));
line=d.readLine();
print("   "+line);
in.close();
d.close();
print("Done...");

But the following code, in which I've added a WHILE loop hangs...

DataInputStream in=new DataInputStream(new FileInputStream("C:\\tmp\\tmp5.txt"));
BufferedReader d=new BufferedReader(new InputStreamReader(in));
while((line=d.readLine())!=null){
      print("   "+line);
}
in.close();
d.close();
print("Done...");

Please help!
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LarryAndro
LarryAndro

ASKER

CEHJ, I removed DataInputStream...

File f=new File("c:\\tmp\\tmp5.txt");
BufferedReader d=new BufferedReader(new FileReader(f));
while((line=d.readLine())!=null){
      print("   "+line);
}
d.close();
print("Done...");

Does this look OK?  (It still hangs.)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It doesn't print anything.  No errors are reported.  Everything just hangs, and I have to stop the Beanshell window and reload.  (The equivalent of a reboot!)  The behavior is identical to being caught in an endless loop... no errors, but nothing happens.

Again, if I remove the WHILE loop, I can d.readLine and print as many lines as are in the text file.  But, when I add the WHILE loop, everything hangs.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To print the first line, it takes two commands...
 
   line=d.readLine();
   print("   "+line);

If there are two lines in the text file, it takes 4 commands...

   line=d.readLine();
   print("   "+line);
   line=d.readLine();
   print("   "+line);

That's what I meant when I said I could print as many lines as existed in the text file.  I should have been more explicit.  Sorry...

And, of course, this approach is a non-starter in "real life" where the number of lines varies!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Before responding, here's some terms...
 
- Console = the DOS or command prompt created by Beanshell.
- Workspace = the Beanshell area for command entry with
                        the "bsh %" prompt.
- Editor = the area where I pasted the code you sent.  I then
                saved and evaluated the code in this editor space.

When I pasted your code into the editor and evaluated, it ran perfectly.  So far, so good!  When I looked at the CONSOLE, the proper output was there.  

Then, I decided to also display the output to the Beanshell WORKSPACE, like this...

... code above not shown ...
    while((line=d.readLine())!=null){
         System.out.println("   "+line);
          print(line);                        <--- new line added
     }
... code below not shown ...

Notice that I added one "print(line);" line of code.

When I ran the above code, everything hung again!  Nothing!!  When I comment the new line it runs.

So, here's the mystifying explanation...

- The print(...) statement outside the WHILE loop worked.
- The print(...) statement inside the WHILE loop hangs.

This is a gotcha to avoid, I guess.  But, do you know why this occurs?

(As far as I'm concerned, this question is answered.  I'll wait for a final reply to my last question about... why does this "feature" happen?... and then will award points.)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The quick answer is...  

PRINT is a built in Beanshell command much similar to System.out.println().

For the official Beanshell documentation, and for the official blurb on Print, go to...  

http://www.beanshell.org/manual/bshcommands.html#print

Using System.out.println vs Print doesn't make a lot of difference to me NOW that I know not to use Print in a WHILE loop.  And, I don't want to waste a lot of your time with meandering follow-up questions.  So, we can draw this question to a close now, if you wish.

For anyone wanting details, a longer answer to the nature of the Print command has been excerpted from...

http://www.beanshell.org/manual/quickstart.html 

in the following two paragraphs...

BeanShell understands standard Java statements, expressions, and method declarations. Statements and expressions are all of the normal things that you'd say inside a Java method such as variable declarations and assignments, method calls, loops, and conditionals.

In the previous example we used a convenient "built-in" BeanShell command called print(), to display values. print() does pretty much the same thing as System.out.println() except that it insures that the output always goes to the command line. print() also displays some types of objects (such as arrays) more verbosely than Java would.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The same problem occurs... execution hangs.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, I couldn't sign on most of the day.  But, back now.  Thanks both of you for your assistance.  Awarding points NOW...  :)
> It's a long-standing one - i've had the same problems before myself

rotfl, amazing he didn't state that earlier :-D
All I care about is the answer.  I suspect CEHJ has the same problem I have... bubble-up memory.  Ask me something, and I don't remember.  Give me 30 minutes and I can slowly fill in details, and often quite accurately.  I guess his memory eventually bubbled to the top!  :)
>>rotfl, amazing he didn't state that earlier :-D

How would that have helped? I wasn't able to solve it myself at the time. As far as i was concerned, we were approaching the problem afresh
Thanks Larry :-)