-1

I'm having a bad time with this error, this code is part of a program that sums the hours and minutes given in two textfields, separated by the symbol ":", the problem is that i can't get the array to store the elements of the StringTokenizer.

Here is the code:

StringTokenizer h1= new StringTokenizer(tx1.getText(),":");
    StringTokenizer h2= new StringTokenizer(tx2.getText(),":");

    int[] arr= new int [h1.countTokens()];
    System.out.println(h1.countTokens());
    int x=0;

    while((h1.hasMoreElements()))
    {   
arr[x]= Integer.parseInt(h1.nextElement().toString())+Integer.parseInt(h2.nextElement().toString()) ;
        x++;
    }

    tx2.setText(Integer.toString(arr[0]));//ignore this line

And here's the error i get everytime:

Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at java.util.StringTokenizer.nextElement(StringTokenizer.java:407)
at progama.pkg1.Proyecto.jButton1ActionPerformed(Proyecto.java:140)
at progama.pkg1.Proyecto.access$000(Proyecto.java:14)
at progama.pkg1.Proyecto$1.actionPerformed(Proyecto.java:52)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

What could be wrong?

Diego RF
  • 11
  • 3
  • Don't use a `StringTokenizer`. What does your text look like? – Elliott Frisch Sep 16 '14 at 00:12
  • Does your `tx2.getText()` have less tokens than `tx1.getText()`? – rgettman Sep 16 '14 at 00:13
  • The input is basically 12:45 in each textfield – Diego RF Sep 16 '14 at 00:13
  • @DiegoRF : Works fine for me if I input the String manually. Also, just for the readability, don't use `nextElement()` but `nextToken()`, and you won't have o use `toString()`. – Dici Sep 16 '14 at 00:16
  • @ElliottFrisch - Why shouldn't s/he use a StringTokenizer? – BadZen Sep 16 '14 at 00:17
  • 1
    @BadZen The [javadoc](http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html) says (in part) *`StringTokenizer` is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.* – Elliott Frisch Sep 16 '14 at 00:18
  • @DiegoRF - Your code assumes the two strings will have the same number of fields. If this is not the case, you will definitely get that exception... do you see why? – BadZen Sep 16 '14 at 00:19
  • @BadZen : the two fields should represent a time with the `mm:hh` format so this assumption is correct. – Dici Sep 16 '14 at 00:20
  • @Dici - And yet... look at the stack trace. If I had to speculate I'd guess maybe the values are coming from some other widgets / text entries / thingers and maybe one is a different length and/or blank... – BadZen Sep 16 '14 at 00:22
  • I will check out the split method, i'm using the Tokenizer class because it was the "required" method according to my teacher, i do not like it at all. – Diego RF Sep 16 '14 at 00:23

1 Answers1

1

First of all StringTokenizer is being phased out according to the java doc. You should use split as follows:

String[] h1 = tx1.getText().split(":");
String[] h2 = tx2.getText().split(":");

int[] sums = new int [h1.length];
for (int i = 0; i < h1.length; i++) {
  sums[x]= Integer.parseInt(h1[i].trim()) + Integer.parseInt(h2[i].trim());
}

The other thing you want to do is to error proof it by handling incorrect input, but I will leave that up to you. You should at least check the sizes of the split arrays and handle non integer (unparsable) inputs.

nmore
  • 2,474
  • 1
  • 14
  • 21