• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Collections.shuffle problem

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

Thank you your comments make sense.

I will in future use lower case names for variables.
 
P White
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic