This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I do not know

 
petr pavlovich
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

why the error I do not know(

array.txt
Console
What's wrong?
please help me.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
petr pavlovich
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
What should I do?
perhaps System.arraycopy()??
 
petr pavlovich
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this is bad?!
100 - static
but can any speaker?
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please UseAMeaningfulSubjectLine next time. You don't want to know how many things I do not know (like how a car engine works).
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic