problemer med StringBuffer og StringTokenizer
der må maks vises 30 tegn per linie men for at undgå halve ord har jeg lavet det her:int buffC = 0;
String tekst = "en længere tekst";
StringTokenizer tok = new StringTokenizer(tekst);
StringBuffer stump;
StringBuffer linie;
public void paint(Graphics g)
{
g.setColor(new Color(0, 0, 0));
g.setFont(new Font("Lucida Console", Font.PLAIN, 12));
while (tok.hasMoreTokens())
{
linie = new StringBuffer();
for (boolean fuld = false; !fuld;)
{
stump = new StringBuffer(tok.nextToken());
if (linie.length() + stump.length() < 36)
{
linie.append(stump+" ");
}
else
{
fuld = true;
}
}
g.drawString(linie.toString(), 24, buffC * 12 + 32);
buffC++;
}
}
det går fint gennem kompileren men når jeg kører det melder den fejl:
java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:259)
at VisualWindow.paint(VisualWindow.java:46)
at sun.awt.RepaintArea.paint(RepaintArea.java:177)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
at java.awt.Component.dispatchEventImpl(Component.java:3678)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)