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

arrays

 
Ranch Foreman
Posts: 880
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I pass an array from main() to a method?
This code is the line that calls the method inside of the console method.  The future array contains Strings.



I don't know what to put inside of the method below to let it know that an array is being passed in.  I don't think that it is String because that would imply that it is a String and not an array.



Thanks,
Kevin
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to specify the parameter type as an array of type String, like this:

Very similar in fact to another method I'm sure is very familiar to you
 
kevin Abel
Ranch Foreman
Posts: 880
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,

Appreciated.  

Now the method public static void is no longer complaining.   The next issue is that I get an error when asking the array to return the value of a cell into a string variable.  


The red x on the left shows this error when I am hovering on it:

The type of the expression must be an array but it is resolved to a string.  

The elements are all strings.  I want to get a value from the array and store it in a String.  What am I doing wroing?

 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:


It looks like future is actually expected to be an array of arrays of Strings, rather than an array of Strings.
 
kevin Abel
Ranch Foreman
Posts: 880
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron,
I want a variable that holds a single element of the array.
I don't know how to do that.
Thanks,
Kevin
 
kevin Abel
Ranch Foreman
Posts: 880
8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim and Ron,

I figured out what Java was complaining about.   I'm passing in a two dimensional array.  I needed to have two sets of square brackets in the method declaration.


public static void displayGrid(String future[][]) {

Thanks,

Kevin
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote: . . . The elements are all strings. . . .

I don't like that sort of design, I am afraid. Two reasons:-
  • 1: The values in your array aren't actually Strings and it would be inappropriate to express them as Strings.
  • 2: Unless you need values to print a JTable, which permits such arrays, you are far better creating an Investment class encapsulating those data and pass an Investment[]. You can readily convert an Investment[] to Object[][] for JTables.
  • Another advantage of an Investment object is that you can validate the values in the constructor
     
    Sheriff
    Posts: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ron McLeod wrote:It looks like future is actually expected to be an array of arrays of Strings, rather than an array of Strings.


    kevin Abel wrote:I'm passing in a two dimensional array.


    What are called 2D arrays in Java are actually an array of arrays.  The difference is that an array of arrays can be "ragged", that is, each of the elements that is an array can have a different size.
     
    kevin Abel
    Ranch Foreman
    Posts: 880
    8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ron,
    The exercise in my murach book is having me work with a two dimensional array.  In each element I want a String.  So I'm not sure of the wording.  
     
    kevin Abel
    Ranch Foreman
    Posts: 880
    8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Campbell,
    I like the idea of having another class.  I am less than half way through the book and I want to keep on moving along.  I want to learn about as many topics as I can.  It's something I can attempt later on.   I appreciate the ideas.
    Thanks,
    Kevin
     
    kevin Abel
    Ranch Foreman
    Posts: 880
    8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Knute,
    Appreciated.
    I am attempting to have a 10X4 sized array containing Strings.
    Thanks,
    Kevin
     
    Campbell Ritchie
    Marshal
    Posts: 79180
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You mean ten 4‑size arrays in an array.
     
    kevin Abel
    Ranch Foreman
    Posts: 880
    8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Campbell,
    That is a better way to say it when working with Java.
    Thanks,
    Kevin
     
    Knute Snortum
    Sheriff
    Posts: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    kevin Abel wrote:I figured out what Java was complaining about.   I'm passing in a two dimensional array.  I needed to have two sets of square brackets in the method declaration.

    public static void displayGrid(String future[][]) {


    That will work, but the more modern way of declaring an array of an array of Strings is this:
     
    kevin Abel
    Ranch Foreman
    Posts: 880
    8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Kunte,
    Appreciated information.
    Thanks,
    Kevin
     
    You guys haven't done this much, have ya? I suggest you study this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic