• 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

populate array from txt file

 
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 looking to populate a multidimensional array with integers that are read from a txt file by using a method.
For example my array looks like this:    

public static void gameBoard(int[][] M) throws Exception{

}

and my txt file has these numbers with a space between each integer:
0 1
1 1
2 4
3 4
4 4
5 3
5 5
5 6
6 1
6 2
6 4
6 6
7 5
7 7

How would I get these numbers into the arrays?  I've tried using a while loop and hasNext() to read the txt file but I havn't been able to successfully populate the array with anything.  Any advice?
 
Marshal
Posts: 26383
81
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
Before you start writing code to "get these numbers into the arrays" you need to find out what that means. (Writing code for requirements that you don't understand is a bad idea.)

Now, maybe you know what that means already. But I sure don't. Your code fragment implies you have a two-dimensional array, although we have no idea how big it is. And there are 24 integers in the input that you posted; are they supposed to go into the array? If so, what are the rules for putting them in? Or is there something else going on with those numbers?
 
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
The integers that I showed are the row and column points in the txt file that have read into my game board.

Here is what I have written so far:



My int [][]initBoard method is where I am having difficulty.  I've tried using hasNext() to read the txt file but my board isnt printing the integers into the row and columns.
 
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
"hasNext()" doesn't read the next token, it just returns true if there is one to read. To actually read it you need "next()".

I think you want to read ints though, so use "hasNextInt()" and "nextInt()".

If your file has a row that says
5 6

Does that mean you want to
array[ 5 ][ 6 ] = 1; // ???
 
lowercase baba
Posts: 12964
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kory Perry wrote:I'm looking to populate a multidimensional array with integers that are read from a txt file by using a method.


This is more complicated than you make it sound.  it's not one thing, it's many.
1) You need to populate many arrays with integers you get from <somewhere>.
2) you need to populate an array of arrays of integers you get from <somewhere>
3) You need to read from a file
4) you need to parse data from that file
5) you need to validate the data in the file (maybe...it depends on how sure you are your file will only contain ints)

There may even be more steps.  But each of the above could really be a method in and of itself.

One of the hardest things to learn is that a method shouldn't do too much.  It's much better, safer, and easier to write a lot of small methods that each do one thing, and do it well, than to write a single, monolithic method that tries to do everything.  It also makes you program more flexible.  If your method that builds the array doesn't have any logic about getting the data, you can change it later...maybe you'll want to read from a database, or a port on a remote server...if the method for getting the data is completely separate from the method that uses the data, you can change one (or even replace it entirely) without worrying about the other.
 
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
Fred, I mostly agree with you except for your step (1). I don't see where you get that from.
 
fred rosenberger
lowercase baba
Posts: 12964
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Fred, I mostly agree with you except for your step (1). I don't see where you get that from.


Java does not have 2-d arrays.  you have an array that holds arrays.  the OP needs to build many small int arrays, that then get put into the int [][].
 
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
OP did this with


Though  elements are not initialized to zero which would have happened if it was declared as a member variable.
EDIT: I was wrong. Even declared as local variable they are initialized to zero.
 
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
Kory, you've got a start there. One change needed right off is to use try-with-resources to open the Scanner. In that way the Scanner will be automatically closed before the method returns.
Also, variable names should begin with a lower case letter.
 
Marshal
Posts: 72066
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you will have to use while (myScanner.hasNextInt()) ... or similar, otherwise you will suffer an exception when you overrun the end of the file.
 
Saloon Keeper
Posts: 2828
149
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a slightly different thought process here...

Does the 1 say that the board cell is occupied ?
What's the use of having a large array of 0s and having only some of them as 1 ?

I would imagine a simple array like this:


where BoardPiece can be:


If there is more than one "state" .. you may want to think about an enum here.
 
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
My post was just a smaller version of what I actually have below.  I was just trying to see if I could get that text file into the arrays and then use that logic with my final code.  The integers in the text file are just coordinates that need to be displayed as *s on the grid.  The M[][] arrays that I have commented out were just coordinate tests to see if everything works.  Since they do work, I'm trying to replace them with the integers in my txt file instead.  I've been scratching my brain for days trying to figure out how to read that text file into the grid and display the *s.  Hopefully that makes more sense to the people that have been trying to help.

 
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
I suggest you start by removing the keyword static everywhere except the main() method and constant declarations. Things static do have their place, but that place is the exception not the rule.
Don't call variables anything like M. It isn't clear what that means, and anyway, only constants should have upper‑case names. Don't put spaces before the [] in array types: int[][] please not int [][].
Don't write if (something) return true; else return false;
Write return c == 'y' || c == 'Y';

As Salvin says, why are you using numbers when you mean a cell is occupied or vacant?
Please provide some explanation of your algorithms; how are you counting neighbours to a square?
Please tell us what problems you are suffering.
 
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
After tinkering around with my while loop, I was able to get everything working.  Thanks everyone for all the help.
 
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
Please show us what you have got.
 
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
First I changed my variables like suggested.  Then I called the coordinates and set my rows and columns to 1.




Here's the game board after the txt file has been read into the grid.

 
Campbell Ritchie
Marshal
Posts: 72066
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not bad
You still have everything static, which I told you yesterday isn't correct.
You are neither opening nor closing your Scanner correctly. Look at the Java™ Tutorials and also find the basic IO section. This is how it should be done:-Use a Path rather than a File object. Don't write close();. You may prefer to rethrow the exception and handle it elsewhere. If so, write a throws instead of a catch.
 
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
To continue from what Salvin said earlier, why are you using numbers and printing them as *s? Why not use an enum?
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic