将StringTokenizer存储在整数数组中

时间:2014-09-16 00:10:24

标签: java arrays stringtokenizer

我遇到这个错误的时间很短,这段代码是一个程序的一部分,它将两个文本字段中给出的小时和分钟相加,用符号"分隔:",问题是因为我不能让数组存储StringTokenizer的元素。

以下是代码:

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

这是我每次都得到的错误:

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)

可能出现什么问题?

1 个答案:

答案 0 :(得分:1)

首先,根据java doc逐步淘汰StringTokenizer。您应该按如下方式使用拆分:

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());
}

您要做的另一件事是通过处理错误输入来对其进行错误验证,但我会将其留给您。您至少应检查拆分数组的大小并处理非整数(不可解析)输入。