• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

trouble populating 2D array

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to populate a 2D array with numbers from a text file.
The problem that I am having is that my array is only printing zeros instead of the numbers from the text file.
I have to copy the array into my method called initMatrix and then send that information to main to print.
Ive been stuck on this for days.  Can anyone please help?

here is the result of my code:




here is the text file:



and here is my code:


 
Bartender
Posts: 7792
70
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
You are printing before you call your initMatrix() method, so it will be zeros.
 
Marshal
Posts: 72066
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, what happens when you run your code? Do you suffer any exceptions? That isn't how you should populate an array from a Scanner.  Has anybody taught you how to use nested loops on arrays?
And the reason you saw all those 0s is that you printed the array before you populated it.
 
Kory Perry
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I tried doing a nested loop but I'm getting an exception error.




here is the modified code
 
Carey Brown
Bartender
Posts: 7792
70
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
Why are you reading row/col from the file? The file does not contain row & col, those are implied by the position of the value.
 
Kory Perry
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats what I thought I had to do.  I've been reading my books, watching videos, looking at other people's code and I cant come up with any solutions.
 
Campbell Ritchie
Marshal
Posts: 72066
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You also haven't written your loops correctly. You appear to be having similar problems to somebody else whom I replied to about 5 minutes ago here.

Right: using fixed numbers in for loops is a bad idea; indeed if your array isn't a plain square array matrix, using rows and columns variables can cause you to read the wrong lengths for your arrays. Remember it is much more serious to read too few values into the array than too many because you won't find out about the missing values until much later.
They ought to have taught you the basic format of a for loop to iterate an array, because
  • 1: Everybody is familiar with it
  • 2: If you don't change the indices inside the loop, this format always works.
  • Here it is:- for (int i = 0; i < myArray.length; i++) {...}
    If you read into the array with such a loop without any changes to the indices, you will fill your array correctly. Remember yuor code is coupled tightly to the format of the text file. This coupling is very difficult to avoid.
    * * * * * * * * * * * * * * * *
    Also, haven't you been taught to close resources? Look at this Java™ Tutorials section which tells you how to open a resource and close it without writing close();
    * * * * * * * * * * * * * * * *
    Of course in the most very recent versions of Java® (“recent”? this feature is nearly seven years old) it has been possible to use a Stream to create arrays. Let's see if I can remember how to do it The Files#newBufferedReader() method takes a Path to the file (Paths.get()) and creates a buffered reader to read it. Since you have read the Java™ Tutorials link I gave you, you will know what try (...) means.
    The lines() method creates a Stream<String> which handles the individual lines of the file. There is a brief introduction to Streams in the Java™ Tutorials, but the best resources I know are Urma Fusco and Mycroft's books Java8 in Action and Modern Java in Action (Manning), which are actually different editions of the same book.
    The map() call converts the Stream of Strings to a Stream<int[]>. It does this by passing each line to a Scanner, which divides it into tokens using its default delimiter. The tokens() call creates another Stream, which is converted to a special Stream for ints with mapToInt(), using the Scanner#nextInt() method. The strange syntax with :: is explained here in the Java™ Tutorials (or in Urma Fusco and Mycroft). A closely related Java™ Tutorials section explains the line with ->.
    It should be obvious what the first toArray() call does.
    Now we have a Stream for int[]s, which can be collected into an int[][] with a different toArray() method and the bit with ::new means it is passed the notional constructor of an int[][].
    Voilà!
     
    Carey Brown
    Bartender
    Posts: 7792
    70
    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
    If you had a text file:
    And you started to read ints from the beginning, what row and column would value 14 be?

    To get back to what Campbell was saying, don't hard code your limits in your loops. You can get your limits directly from your array size.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic