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

I do not know

petr pavlovich
Greenhorn

Joined: Jul 10, 2011
Posts: 22
Hello!

why the error I do not know(

array.txt
Console
What's wrong?
please help me.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2


You have initialized your days array to have 100 Strings.
After reading your text file, your days array filled with 7 elements.


This will actually iterate 100 times (the array size) instead of 7 (the filled content length).
When it move to an element (String) which has not been initialized (say 99th element).

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You created an array of a 100 elements yet only fill 7 elements. The rest of the elements will remain null.
Then if you sort it you're trying to invoke a method on a null reference.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
petr pavlovich
Greenhorn

Joined: Jul 10, 2011
Posts: 22
thanks
What should I do?
perhaps System.arraycopy()??
petr pavlovich
Greenhorn

Joined: Jul 10, 2011
Posts: 22

this is bad?!
100 - static
but can any speaker?
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

normally, you use array if you know the exact size of content to be stored.
if not, other collection class could be better choice.

Is using Array a requirement?
If not, there is a collection class which could do sorting and maintain uniqueness for you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
Find some way of getting a 7-element array. This is one wayThis is another\s+ is a regular expression for one or more whitespace characters. I think I have got it right, but might be mistaken.
You can add the days individually to a List<String> and get an array out of the List; List has several methods which you can use for that.

And it would be better if we knew what the question is about.
petr pavlovich
Greenhorn

Joined: Jul 10, 2011
Posts: 22
Raymond Tong wrote:normally, you use array if you know the exact size of content to be stored.
if not, other collection class could be better choice.

Is using Array a requirement?
If not, there is a collection class which could do sorting and maintain uniqueness for you.


I do not know the size of the array.
If not, there is a collection class which could do sorting and maintain uniqueness for you
?)
and can be more detail?)
petr pavlovich
Greenhorn

Joined: Jul 10, 2011
Posts: 22

maybe arraycopy

public static void arraycopy(Object source, int sourcePosition,
Object destination, int destinationPosition, int numberOfElements)

src: The source array.
srcPosition: The position in the source from where you wish to begin copying.
des: The destination array.
desPosition: The position in the destination array to where the copy should start.
length: The number of elements to be copied.
and length = days.length???



so maybe???
petr pavlovich
Greenhorn

Joined: Jul 10, 2011
Posts: 22
petr pavlovich wrote:
maybe arraycopy

public static void arraycopy(Object source, int sourcePosition,
Object destination, int destinationPosition, int numberOfElements)

src: The source array.
srcPosition: The position in the source from where you wish to begin copying.
des: The destination array.
desPosition: The position in the destination array to where the copy should start.
length: The number of elements to be copied.
and length = days.length???



so maybe???


ox no(
days.length = 100 and not 7)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

It's been suggested before but: Look at the collection classes. For instance ArrayList or HashSet and look at the Collections class and what it can do for you.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

And please UseAMeaningfulSubjectLine next time. You don't want to know how many things I do not know (like how a car engine works).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

petr pavlovich wrote:I do not know the size of the array.
If not, there is a collection class which could do sorting and maintain uniqueness for you
?)
and can be more detail?)

Collection classes - List, Set, Map ...
They are slightly different to each other.
Yourself should know the requirement and could pick the most suitable implementation class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I do not know