[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 522
  • Last Modified:

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!
0
LarryAndro
Asked:
LarryAndro
  • 8
  • 8
  • 4
9 Solutions
 
CEHJCommented:
Try getting rid of the DataInputStream
0
 
LarryAndroAuthor Commented:
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.)
0
 
CEHJCommented:
Yes that's OK. Does it print out all the lines?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LarryAndroAuthor Commented:
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.
0
 
CEHJCommented:
>>I can d.readLine and print as many lines as are in the text file

how do you do that *without* a while loop?
0
 
LarryAndroAuthor Commented:
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!
0
 
objectsCommented:
That code shouldn't  hang, check the problems not actuall elsewhere

try {

File f=new File("c:\\tmp\\tmp5.txt");
System.out.println(f.length());
BufferedReader d=new BufferedReader(new FileReader(f));
System.out.println("About to loop");
while((line=d.readLine())!=null){
      System.out.println("   "+line);
}
d.close();
print("Done...");
} catch (Exception ex) {
   ex.printStackTrace();
}
0
 
LarryAndroAuthor Commented:
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.)
0
 
objectsCommented:
so what exactly is print() doing (what class is it being called from)
0
 
LarryAndroAuthor Commented:
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.

0
 
CEHJCommented:
Try

while((line=d.readLine())!=null){      
          print(line + "\n")
}
0
 
LarryAndroAuthor Commented:
The same problem occurs... execution hangs.
0
 
CEHJCommented:
Not sure why their own method should ...
0
 
objectsCommented:
sounds like a bug to me.
0
 
CEHJCommented:
It's a long-standing one - i've had the same problems before myself
0
 
LarryAndroAuthor Commented:
Well, I couldn't sign on most of the day.  But, back now.  Thanks both of you for your assistance.  Awarding points NOW...  :)
0
 
objectsCommented:
> It's a long-standing one - i've had the same problems before myself

rotfl, amazing he didn't state that earlier :-D
0
 
LarryAndroAuthor Commented:
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!  :)
0
 
CEHJCommented:
>>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
0
 
CEHJCommented:
Thanks Larry :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now