-1

I'm trying to use the stopwatch class to measure the speed at which my code runs on ArrayList vs. LinkedList. I continue to get this error when I try to compile my code.

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)

My code looks like this:

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

my command line input looks like this:

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

I tried initializing N to 0 and 1, and the error wasn't there anymore. As soon as I set N to 2, the error came back. So what I'm getting from this is there's something wrong with N > 1. But I have no idea what exactly is causing the error. Help would be appreciated.

Bianca Fuller
  • 39
  • 1
  • 9

1 Answers1

2

Why don't you try debugging it yourself. Here's how you can get started.

for(int i = 0; i < args.length; i++){
  System.out.println("Arg "+i+" is: "+args[i]);
}
satnam
  • 10,719
  • 5
  • 32
  • 42