0

My question is, why do I get an Exception in thread "main" java.lang.ArrayStoreException :

at java.lang.System.arraycopy (native method) at java.util.ArrayList.toArray (unknown source) at Main.main (Main.java:50)

Here's my code :

import java.io.*;
import java.util.*;

public class Main {

static public boolean readPinsData(File dataFile, ArrayList<Integer> data) {
   boolean err = false;
   try {
      Scanner scanner = new Scanner(dataFile);
      String line;
      while (scanner.hasNext()) {
         line = scanner.nextLine();
         try {
            data.add(Integer.parseInt(line));
         } catch (NumberFormatException e) {
            e.printStackTrace();
            err = true;
         }
      }
      scanner.close();
   } catch (FileNotFoundException e) {
      e.printStackTrace();
      err = true;
   }

   return err;
}


public static void main(String[] args) {
    Console console = System.console();
    int pinSize = 0;

    do{
    char passwordArray[] = console.readPassword("Enter pin: ");
    pinSize = passwordArray.length;

    if(pinSize != 4){
            System.out.println("Pin must be 4 digits");
        } else {
            System.out.println("Checking...");
        }


    ArrayList<Integer> pins = new ArrayList<Integer>();
   readPinsData(new File("bdd.txt"), pins);
   //System.out.println(pins);
   //System.out.println(passwordArray);

   String[] thePins = pins.toArray(new String[pins.size()]);
   String passEntered = String.valueOf(passwordArray);
   int i = 0;

   for(i = 0 ; i < thePins.length ; i++){
      if(passEntered == thePins[i]){
          System.out.println(":)");
      }else{
          System.out.println(":(");
      }
  }

   }while(pinSize != 4);

}
}

My bdd.txt file looks like :

1111
2222
3333
4444
5555
6666
7777
8888
9999
hacks4life
  • 691
  • 5
  • 16
  • 38
  • 1
    Another problem is that you're comparing Strings with `==`. Use equals() instead. And use collections rather than arrays. There's no reason to transform your list to an array: you can iterate on a list directly. – JB Nizet Mar 21 '14 at 13:04
  • I do not see how to use collections. What do you mean by that ? I need the size of the List to loop on it, right? – hacks4life Mar 21 '14 at 13:06
  • An ArrayList is a collection. You can iterate through an ArrayList. There's no need to transform the list to an array in order to be able to iterate on it. Look at your code: it reads strings from a file, then transform each string to an Integer and stores the Integer in a list, then it transforms the list of integers into an array of strings. Why not read the lines into a list of strings, and use list.contains(passEntered) to check if the list contains the entered password? What's the point of all these transformations? – JB Nizet Mar 21 '14 at 13:12
  • I see, actually I spent hours on my code and I did not realize this was not the better way to do it. I was a bit lost. – hacks4life Mar 21 '14 at 13:19

1 Answers1

6

Basically you've got a List<Integer> and you're trying to store the contents of it in a String[]. You can't do that. If you want to convert each Integer into a String, you'll need to do that explicitly.

For example:

String[] thePins = new String[pins.size()];
for (int i = 0; i < thePins.length; i++) {
    thePins[i] = pins.get(i).toString();
}

Or build a List<String> instead of using an array.

Or don't bother converting everything to a string collection at all - instead, just iterate over pins and test that way.

As noted by JB Nizet, you should also use equals rather than == when comparing strings.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I tried to convert it this way "String[] thePins = pins.toArray(new String[pins.size()]);". Do I need to loop on each pins' Integer ? – hacks4life Mar 21 '14 at 13:01
  • @FlorentP: Yes we can see what you tried to do, because that's in the question :) You can't do that - you need to iterate over the integers and call `toString()` on each one. – Jon Skeet Mar 21 '14 at 13:03
  • But how to do loop on the List ? List.size() does not work apparently. – hacks4life Mar 21 '14 at 13:05
  • @FlorentP: Yes, `List.size()` very definitely *does* work. I'll edit my answer to show how you could do this. – Jon Skeet Mar 21 '14 at 13:07
  • Also, how can I modify the end of my code, to print only one :) or :( (depending of the pin entered by the person). I am bit confused – hacks4life Mar 21 '14 at 13:22
  • @FlorentP: That sounds like a different question - and one you should think about more first, then ask as a *new* question if you can't figure it out. Consider stepping through your code with a debugger, and work out what you'd *like* to do... – Jon Skeet Mar 21 '14 at 13:27
  • Yes actually when I try to import the file into Eclipse, there is a java.null.pointerException with the "char passwordArray[] = console.readPassword("Enter pin: ");". That is why I don't use IDE (and a debugger). – hacks4life Mar 21 '14 at 13:30
  • @FlorentP: That just means your code is broken - it's assuming that `System.console()` always returns a non-null value. Also, it shows that your code isn't particularly testable at the moment - if you split this up more, you could write appropriate unit tests and step through *those*. – Jon Skeet Mar 21 '14 at 13:36
  • Well thank you, I have just used "break;" in my conditions, it stops immediatly and print only one smiley. You suggest me to split the code into functions ? I am really not at ease with JUnit, Mockito etc... – hacks4life Mar 21 '14 at 13:49
  • @FlorentP: If you're not comfortable with unit testing yet, I suggest you spend time *becoming* comfortable with it - at least if you're expecting to do coding in any kind of serious way. The sooner you get the hang of it, the better. – Jon Skeet Mar 21 '14 at 13:50
  • I have heard of that lol. Actually it is for my 6-months internship in NFC Security on Android Device. Thus I know I will have to deal with unit testing unfortunately. – hacks4life Mar 21 '14 at 13:52
  • Jon, where do you suggest me to go in order to start learning unit testing? – hacks4life Mar 24 '14 at 14:53