【问题标题】:java.lang.NumberFormatException error in eclipse when parsing an int解析int时eclipse中的java.lang.NumberFormatException错误
【发布时间】:2015-03-25 00:31:19
【问题描述】:

我正在尝试使用秒表类来测量我的代码在 ArrayList 与 LinkedList 上的运行速度。当我尝试编译我的代码时,我继续收到此错误。

Exception in thread "main" java.lang.NumberFormatException: For input string: "java"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at SortTest.main(SortTest.java:10)

我的代码如下所示:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;


public class SortTest {

public static void main(String[] args) {

    final int N = Integer.parseInt(args[0]);
    ArrayList<Integer> thelist = new ArrayList();
    int x = 1;
    while (x < N) {
        thelist.add(x);
    }

    final Random RNG = new Random (Long.getLong("seed", System.nanoTime()));
    Collections.shuffle(thelist, RNG);

    /////////////
    //STOPWATCH//
    /////////////

    final StopWatch sw = new StopWatch(true);
    for (int i = 0; i < 25; i++) {
        sw.start();

        int smaller = 0;
        int smallerindex = 0;
        int b = 0;
        for(int a = 1; a <thelist.size(); a++) {
            smaller = thelist.get(a - 1);
            smallerindex = a - 1;
            for(b = a; b <thelist.size(); b++) {
                if(thelist.get(b) < smaller) {
                    smaller = thelist.get(b);
                    smallerindex = b;
                }
            }
            if (smallerindex ==  a) {
                //no action
            } else {
            int temp = thelist.get(a);
            thelist.set(a, thelist.get(smallerindex));
            thelist.set(smallerindex, temp);
            }  
        }

        sw.stop();
    }
    final double averageTime = sw.getAverageTime();

    System.out.println(averageTime);
}
}

我的命令行输入如下所示:

java -Dseed=4321567 SortTest 200 java.util.ArrayList

我尝试将 N 初始化为 0 和 1,并且错误不再存在。一旦我将 N 设置为 2,错误就回来了。所以我从中得到的是 N > 1 有问题。但我不知道究竟是什么导致了错误。帮助将不胜感激。

【问题讨论】:

  • 请考虑接受您之前问题的一些答案。您已经问了 8 个,收到了 7 个的答案,但没有接受任何答案。

标签: java parsing sorting stopwatch seed


【解决方案1】:

您为什么不尝试自己调试它。以下是您可以开始的方法。

for(int i = 0; i < args.length; i++){
  System.out.println("Arg "+i+" is: "+args[i]);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-29
    • 2016-11-26
    • 2011-03-23
    • 2011-07-17
    • 1970-01-01
    • 2018-10-11
    • 2016-01-03
    • 1970-01-01
    相关资源
    最近更新 更多