File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Collections.shuffle problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collections.shuffle problem" Watch "Collections.shuffle problem" New topic
Author

Collections.shuffle problem

P White
Greenhorn

Joined: Dec 28, 2011
Posts: 10
Hi,

I have almost finished a program to create and take quizes.

The quiz creator is finished but I have problem with the quiz taker.

The quiz file is a basic .csv file

To stop the student from reading the .csv file I am encrypting the file.

1. I am reading the csv file replacing \r\n with a a ¬, replacing \n with ~, and then replacing the ¬ with \r\n
2. I am then encrypting the file.

When the quiz runs I am decypting the file, replacing the ~ with \n and returning the data to csv format.

The questions and answers display correctly but I want to shuffle the questions

As soon as I add the collections.shuffle into the code I get this error

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at quiz.ReadEncryptedFile.ReadCSVFile(ReadEncryptedFile.java:110)
at quiz.UserInterface.main(UserInterface.java:194)

I think this is caused because the collections.shuffle has not completed.
I have tried to make the program pause but this just delays the error from appearing.

My csv file reader contains this code which is where the problem lies.


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

I'd suggest confirming exactly how many entries there are in data just before you call shuffle, because I suspect there's no more than one entry, which (as you can probably imagine) will confuse a shuffle algorithm.
P White
Greenhorn

Joined: Dec 28, 2011
Posts: 10
Matthew,

Thank you for the time you have taken to look at this for me.

If I add a a counter and a line to printout the information contained in "data" like this there are 128 lines of data.

Also if I add a line to print "data" following reader.close() I can see all the data up to and including the last line from my file.

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

Are you sure the exception is occurring when you shuffle? I ask because the stack trace you gave in the first post doesn't contain a reference to Collections.shuffle, which I would expect to if the exception happened within that. Or are you omitting part of the stack trace? That trace tells you that the error occurs on line 110 of ReadEncryptedFile.java, but I can't tell which that is.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

If it isn't too long, could you post the code for ReadCSVFile.java? The stack trace suggests you are attempting to access the first element of an empty list.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Can you explain what the if-statement at line 3 of your posted code is supposed to do? (Line 3 in the original post, line 4 in the later post.) As far as I can see it doesn't do anything useful and it's possible it can incorrectly cause the code inside it to not be executed. Which would lead to your list being empty... which in turn would cause careless code which assumed the list wasn't empty to crash in the way your code is crashing.
P White
Greenhorn

Joined: Dec 28, 2011
Posts: 10
This is the entire code from my csv file reader

I have been experimenting and there are some reduntant lines of code i.e. boolean ready = false:
FYI I was trying to use wait() to allow the shuffle to complete but this concept is completely beyond me................

If I enable the index counter just above I get a seemingly random count and then the error at ColQ.add(temp.get(0));
This is always the line indicated.

Please bear in mind I only get an error when I enable the line "Collections.shuffle(data);

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at quiz.ReadEncryptedFile.ReadCSVFile(ReadEncryptedFile.java:115)
at quiz.UserInterface.main(UserInterface.java:194)

Line 3 should only allow the file to be read if it exists and can be read - this bit (I think) is not causing me a problem, even if it not the way to check.
I have disabled it anyway and tried agian but am still getting the same error.

Thank you all very much.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

P White wrote:Line 3 should only allow the file to be read if it exists and can be read


Then why does it follow a line which attempts to read the file? That's kind of locking the barn door after the horse has run away. And why does it refer to a variable name which has nothing to do with the actual file being read?

By the way, you may not have noticed that moderators have been editing your posts to put code inside the "Code" tags. Notice how much less readable your last post is, without those tags? It would be nice if you could do that yourself in the future.
P White
Greenhorn

Joined: Dec 28, 2011
Posts: 10
Paul,

Thank you for your patience.
I have moved the code as you suggest to make it readable I hadn't realised you needed to put it inside the bracket.

Looking at my code I see your point. I am new to this and have trouble passing vaiables between classes (sometimes it seems to work and sometimes it doesn't) hense you will find hard coded bits referring to variables in an another class. This is what happened here the fliename was not passed so I hard coded it as a work around.

I am trying to learn java by creating working apps this is my third.

The code is not pretty I know but until I have spent more time writing bits and peices I will never get to the bottom of my errors and find the solutions to them.

And of course a thank you to everyone else who has taken the time to try to help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Then let me point out a few more difficulties in your code. For example:


I looked at that and thought "What?? The File class has a readAsStrings method? Is this some new Java 7 feature I missed?" (Which is a real possibility, by the way.) But then I looked around a bit and saw this:



So now it makes sense to me. There's a reason that the Java standard coding conventions say that variable names should start with lower-case letters; that way people don't get variable names confused with class names (which should start with upper-case letters).

And then there's the matter of your ReadEncryptedFile. What's up with that name? It's normal for the names of Java classes to be nouns; a class represents objects, which are of course things. Whereas "ReadEncryptedFile" is the name of an activity, not the name of a thing. I would have used "EncryptedFile" as a name, since that could be the name of a thing.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

And let's have a look at these two lines:



Here you declare a variable "temp" and assign it a reference to a new, empty, ArrayList. In the second line you throw away that ArrayList and assign "temp" a reference to (presumably) an ArrayList returned by the "extractFromCommas" method. So what was the point of the first assignment? Just do this:

P White
Greenhorn

Joined: Dec 28, 2011
Posts: 10
Paul,

Thank you your comments make sense.

I will in future use lower case names for variables.
P White
Greenhorn

Joined: Dec 28, 2011
Posts: 10
Guys,

Thanks for all your help.

I have taken all advice on board and changed the variables to lower case.

I took a break from this and went to the pub last night where I had a thought. What if I put in an extra step?
My thoughts were that I was not sure how the underlying Java system was handling adding the extra linefeeds then shuffling during one operation. Were the extra line feeds a problem?

So I changed the code to:



I initially got an error reading the array as ot counted to 130 and then errored so I put a -1 on the count to stop this. My file has only 128 lines.
This seems to have cured the problem.

I thought you might like to know the solution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections.shuffle problem