How can I set this template so that lengthy code doesn't overshoot the page?

I actually asked the exact question here: http://tex.stackexchange.com/questions/87646/problem-formatting-java-code-in-my-latex-document-overshooting-the-page

However I have tried using the solution I was given (which did work for the example I used them) but it seems that this does not work with the package I want to use (example below).

I have looked through the texdoc file for captions and floats and could not find anything on how to do it either.

\usepackage{float}
\usepackage{caption}
\begin{document}
\floatstyle{ruled}
\newfloat{program}{thp}{lop}
\floatname{program}{Program}
\begin{program}
\begin{verbatim}

if(args.length>0){
while(expression){

BufferedReader yReader=new BufferedReader(new FileReader(IOUtil.fileName(args[1]))); } }

\end{verbatim}
\caption{}
\end{program}
\end{document}

-

This redefines the verbatim space to allow a break:

\documentclass{article}
\usepackage{float}
\usepackage{caption}
\begin{document}
\floatstyle{ruled}
\newfloat{program}{thp}{lop}
\floatname{program}{Program}
\begin{program}

\makeatletter
\def\@xobeysp{\ \hfil\penalty\z@\hfilneg}
\begin{verbatim}

if(args.length>0){
while(expression){

BufferedReader yReader=new BufferedReader(new FileReader(IOUtil.fileName(args[1]))); } }

\end{verbatim}
\caption{}
\end{program}
\end{document}

-
Hey that worked the only problem is it trailed to the bottom of the page and beyond. Any idea how I can stop that too? – Magpie Jan 27 '13 at 16:26
program is a float environment so a minipage so never breaks just put the verbatim in the normal text run if you want it to break over a page you can use the caption package to do captionof{program}{...}to caption the non-floating verbatim – David Carlisle Jan 27 '13 at 16:31
Ah right. I think I had this once before thanks for clarifying. – Magpie Jan 27 '13 at 16:35