• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

problems creating a new larger array with one extra element

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am creating a program to have information entered into it and will store that into a csv file. I figure if I set rowSize variable at the top of the class so it covers the scope of the program I should be able to call a method to increment rowSize of the array and insert the new element into the new part of the array created. The program runs smoothly if I don't try and add the code to increment the rowSize and w (lines 144 & 145) but when they're run I get index out of bounds exceptions at line 150. What I'm I doing wrong to create this growing CSV file?

With the Error/output

Sorry for the length.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arrays in Java have a fixed size. If you want to resize an array you need to create a new one the size you want, and copy all the elements across to it.

Or, a much better solution: use a more appropriate data structure. You want a List (either an ArrayList or a LinkedList), which is designed to allow the size to change.
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That seems like it's going to be a lot of rework. Is there a way that I can convert the arrays I already have to an arraylist?
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I bet I could do something like this.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dustin Schreader wrote:I bet I could do something like this.

Possibly, but it seems like an awful lot of work to cover up for the fact that you've made a bad decision to start with.
My advice: follow Matthew's.

Is there a way that I can convert the arrays I already have to an arraylist?

Yes, but it doesn't alter the fact that you're better off with an ArrayList to begin with.
Sometimes the school of hard knocks is the best way to learn, and the lesson here is:
unless you know how many items you're going to be creating, always use a collection rather than an array.

Winston
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright I'll get working on it. Thank you much for the help!
 
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
If you're re-working it ...

...there's another big improvement you could make. You're currently using an array of arrays to represent your rows. What I'd suggest is to create a class representing a single row (called Patient - or whatever makes sense in your application). Give that class fields according to what the columns mean. Then you can use an ArrayList<Patient> to hold them in. It will make your code far more readable and maintainable, which is always a good idea.
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds pretty cool but I'm pretty overworked and tired would you be able to provide me with a small sample so I can understand a little better. I guess I don't understand creating a whole new class for just patient information formatting.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dustin Schreader wrote:I guess I don't understand creating a whole new class for just patient information formatting.


Well, something likemight be a start.

However, I suspect that the real problem you're having is that you're spending too much time coding, and not enough thinking about the problem.

You're also not doing yourself any favours with your cryptic codes and field names. What is "pt" for example?

Winston
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So since I had a an array of arrays previously and now I want to use an arrayList it seems easy enough to add more and more elements to an ArrayList but how do I sort through the list like i did when I was using a for loop to sort through the array of arrays? It seems like I'd be sorting through each entire string of combined variables created by my patient class when, let's say I just wanted to compare against the code of the pt to do a search on them not the code, name, physician, floor, and time.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dustin Schreader wrote:It seems like I'd be sorting through each entire string of combined variables created by my patient class when, let's say I just wanted to compare against the code of the pt to do a search on them not the code, name, physician, floor, and time.


I suspect you need to go through the tutorials for object order and sorting. You should also look at java.lang.Comparable and java.util.Comparator.

Winston
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you I will take a look.
 
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beware of long lines in code tags; I have split some of your output into multiple lines so they will fit onto the width of my screen.
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic