Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Static Initialize a 5x6 array

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to read a 5x6 array of Integers from a file and then later static initialize the array within the .java file. A snippet of the input is:
1107 1993 2651 3080 3288 3482
1447 2489 3136 3404 3319 2914
1528 2446 2786 2592 1913 800

So I have to ouptut the numbers in the proper format that can be compiled, i.e. {{{{{1107, 1993, 2651, 3080, 3288, 3482}, for this array declaration:

I thought this code might work.

I can then use the generated output and use it in the .java file. Any thoughts about this?
 
Marshal
Posts: 76487
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There ain't no such thing as a 5×6 array in Java┬«. There is however such a thing as a 5‑element array whose elements are arrays each happening to have 6 elements.

But that isn't what your code shows. You show an array of arrays of arrays of arrays of arrays. There is something wrong with having the array as a static field, and even worse that it is public. The code you showed looks pretty horrible: all that nesting and the multiple ifs are pretty error‑prone.
Start with
System.out.println(Arrays.deepToString(myArray));
and see how you get on with that.
 
Campbell Ritchie
Marshal
Posts: 76487
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On second thoughts: if you are trying to write code from that file, don't write the individual elements. Get yourself a List<String> to contain the text. Create the array from the file, and add the text to the List in the appropriate place. Try deepToString and replacement of [ by { and ] by }. If you use methods like String#replace beware; you will probably have to escape [ as "\\[".
Don't try to write things a letter at a time. Put everything into a StringBuilder and then you can make one print call. It will be much quicker.
 
Rancher
Posts: 1050
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before I give you my thoughts, please verify you have going to have a 5x6 dimension array--7776 elements and not a 5x6 2 dimensional array [5][6] with 30 elements.
 
Eric Racin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, it is a 5x6 dimension array: number of elements is 6x6x6x6x6 = 7,776. I think the code above will work, but thought there might be a more clever way to do it rather than have the nested if statements (case statements instead?). Also, can't tell if it works until I initialize the array: bug messages may not tell me how I missed placement of a '{'. (And if you suggest not using this type of array to handle the data, the code that works around it has been working for 5 years and I don't want to change it).
 
Saloon Keeper
Posts: 9574
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another approach would be to address elements of a multi-dimensional array mathematically, facilitated by helper methods.

 
Marshal
Posts: 27379
88
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
The code you posted would "work" if your goal was to generate some Java source code which could later be incorporated into a Java class and compiled. However it's unnecessarily fussy and could be simplified like this:



But is that really your goal? Since you've already read the multi-dimensional array from a file and have it all set up, I don't really see the point of generating Java code with that same data hard-coded. Wouldn't it be simpler to just re-read the array from the file in that other Java code?

 
Eric Racin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul! Your logic is cleaner than mine.
I decided to declare the array in-line since the Servlet that was working well in Server 2003 cannot read from the local disk in Server 2012. After struggling with getting Tomcat user ID privileges to access the local drive, I decided to do it the cludgy way and just have the whole array inside the .java file.
 
Paul Clapham
Marshal
Posts: 27379
88
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
I'm guessing that your servlet can't read from the local drive because of authentication issues? The way to deal with that is to run the Tomcat service under a specific user ID (not the generic "local server user" ID which is the default) and then to change the security of that file so that it's readable by that specific user ID.
 
Get me the mayor's office! I need to tell him about this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic