• 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

Reading Values from Files

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I am new to this forum and I am also a beginner in Java so please go easy on me haha. I just need some ideas with this problem I'm having:

I am creating a game similar to minesweeper, but instead of mines, it has bees (beesweeper xD). I have to pass in a file to my code which contains the locations of the bees. In the file, the bee locations are written like coordinates (ex: 0 1). The file will typically have multiple coordinates for the bees (more than 1 location). I am having difficulty on how to read these values and store them in a variable. I am using a while loop which checks for .hasNextInt() and then I use .nextInt() to read each integer and store them. Since the bee coordinates have multiple integers, I obviously can't store them in a single integer variable. I was thinking of storing them in an integer array. Thoughts? (I would also like to point out that there is not a set number of bee locations - it changes depending on the file given).

Another thing: I also need the game board to update after a command that the user enters. I don't need exact code or anything, I just want some ideas for how to go about this. Would I use a for loop? If statements?

I also have another problem. I have 2 classes and I am kind of confused on how to call a class from another class? Would it be fine to call my constructor that's in class B from class A, and then run only class A?

This is my first time making a game with Java so I appreciate any help. Thank you!
 
Bartender
Posts: 7794
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
Welcome to The Ranch!

You may want to change your user name, this site requires that you use your real name. Somebody's bound to notice.

Anyway... Is the board a fixed size? Often for this game a board is allocated as a 2D array of arrays of known dimensions and then as the coordinates are read from the file the corresponding 2D cell is marked somehow. This is easier than trying to manage a list of coordinates of unknown size.
 
Aria Villano
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad, I didn't know that. Regarding the board size, it is not fixed - the user determines the size (I wish it was fixed, it would make it much easier haha).
 
Marshal
Posts: 72074
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch (again)

How are you reading your file? If you say nextInt(), that suggests you hve a Scanner object. Although the thread title mentions reading, you didn't say whether you are having problems with reading or recording the numbers. Have you considered a location class with fields x and y?
 
Carey Brown
Bartender
Posts: 7794
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
Do you have any say over the file's format? Could, for instance, the first two number specify the width and height of the board? That would make things so much easier. If not, then where would you get the board dimensions?
 
Carey Brown
Bartender
Posts: 7794
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 the user determines the board size somehow (?), what happens when the size they choose is smaller than the coordinates of the file you are trying to read?
 
Aria Villano
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do have a scanner object which I created before the while loop. So, I guess I'm having trouble with storing the values. Could I just make an int array to store the coordinates? And no, I did not consider making another class because it's required to only have 2 classes.

I also do have a say in the format of the file. The first 2 numbers will always be the width and the height of the board; the 3rd number is the number of bees; and then the remaining numbers are the coordinates for the bees. The file format will always be the same, I just don't know what the numbers are. I do not need to account for if the size is smaller than the coordinates because no files will be formatted like that.

Here's an example of what the file would contain:
10 10
2
0 1
2 3
This would be a 10x10 board with 2 bees located at 0,1 and 2,3.

 
Campbell Ritchie
Marshal
Posts: 72074
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aria Villano wrote:. . . . Could I just make an int array to store the coordinates?

Yes, but how will you know what size to make the array? As somebody has suggested, you might be better off with an int[][]. You might have to read the file twice, the first time to count the lines. In this instance, if you have a count in the file, that will tell you the size of array you need. Or you can use a List, if you know about Lists. Beware of making your array too large, otherwise you are liable to have several (0, 0) pairs at the end of it, which will put numerous bees in the top left corner.

. . . it's required to only have 2 classes. . . .

I am afraid I think that means over‑specifying the assignment; I also think restricting you to two classes forces you into poor design.
 
Aria Villano
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in a correctly formatted file, the size of the array would be double the amount of bees, right? I only want to store the locations in the array, as I have already stored the size and the number of bees. For example, if there were 2 bees, there would be 2 coordinates, and therefore 4 numbers - so the size of the array would be 4. If there were 10 bees, there would be 10 coordinates and a total of 20 coordinate integers. So, would it not work if I did this (my array is called beeLoc):
int[] beeLoc;
beeLoc = new int[numBees * 2];
 
Carey Brown
Bartender
Posts: 7794
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
Well, a couple of things here.

You could have one long array with: x1, y1, x2, y2, x3, y3, ...

Or you could have a two dimensional array: { {x,y}, {x,y}, {x,y}, ... }

Or you could have an array of Location objects: Location(x,y), Location(x,y), Location(x,y)...

Or you could not store them at all but immediately assign them to positions on the board.

Now, back to my other question. If you have a 2D board.
Then if your file has: x=12, y=15; what do you do? That coordinate is outside the 10x10  bounds of the board.
 
Carey Brown
Bartender
Posts: 7794
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
It would seem to me that the board dimensions must be included with the file data because the file data is only valid within the constraints of certain board dimensions. Usually this game populates the board randomly and doesn't run into this issue but you're reading it from a file.
 
Aria Villano
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did something similar to the code you posted but I used a char array because the squares are supposed to be blank before they're revealed. I also have a 2nd array that is of type boolean for the bee locations and is supposed to return true if there is a bee in that location. And the file won't have coordinates that are not on the grid. However, if the user enters a coordinate to reveal or mark a square that is not on the grid, then that will be handled.  
 
Carey Brown
Bartender
Posts: 7794
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

Aria Villano wrote:And the file won't have coordinates that are not on the grid.

If the user enters the board dimensions at runtime, how can you say that?
 
Campbell Ritchie
Marshal
Posts: 72074
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aria Villano wrote:. . . I used a char array . . .

Why? That sounds weird; you are given ints for your locations. That sounds like making things more complicated than they have to be.
How are you handling locations outwith the confines of your grid?
 
Aria Villano
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that issue won't happen because my instructor said that that won't be a problem. And yeah, you're right about the char array. The only reason I did that is because the squares need to be blank before they're revealed and I make them blank with whitespace (like: ' '). The only other option to be able to make them blank would be with a String array. So should I make a third array that's of type int?
 
Carey Brown
Bartender
Posts: 7794
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
No, I was just using 'int' as a place holder to illustrate a point. I was think of something more like you boolean bees[][] array.
 
Aria Villano
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I see. I already have a boolean array for the bees though, plus the char array.
 
Carey Brown
Bartender
Posts: 7794
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 haven't yet shown us any of your code, now might be a good time. When you post code here please UseCodeTags (<---Link).

I was guessing you had a 2D array of bees for your board, something like...
Then later when you try to read your file...
No need to store the row,col in its own array.
 
Campbell Ritchie
Marshal
Posts: 72074
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aria Villano wrote:. . . . So should I make a third array that's of type int?

No. The more arrays you have the more can go wrong. You do not need to store space characters; you simply need to display them. Work out how to take an array and display a space if the element is 0/false. Maybe you can find a Unicode character for a bee and display that instead

Try this Unicode page for the symbol for a honeybee=🐝. Try the appendCodePoint method of StringBuilder to produce that glyph.
 
Carey Brown
Bartender
Posts: 7794
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
Sorry, I swapped the x and y, so should have been...

 
Aria Villano
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I basically did what you did except I have:


And my while loop has one more statement than yours which is:

Campbell - Thanks for the advice, I did not think about it like that. I will probably do it that way then.


 
Aria Villano
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also forgot to mention that numBees has already been declared after my arrays.
 
Carey Brown
Bartender
Posts: 7794
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

Aria Villano wrote:I basically did what you did except I have: ...

As a thread discussion continues it is often necessary to repost your code to make sure everyone's in sync again.
 
Campbell Ritchie
Marshal
Posts: 72074
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Sorry, I swapped the x and y . . .

You and Carey have both demonstrated how easy it is to get confused with multiple arrays and their indices

That loop is better, but still incorrect. Where does the − 1 come from? Look in the Java™ Tutorials for how to create a Scanner reading a file, and how to close it without writing close(); And remind yourself of the basic form of a for loop. for (int i = 0; i < myArray.length; i++) {...} There are good reasons for using that format:-
  • 1: Everybody is familiar with it.
  • 2: It always works if the loop doesn't change any of the indices.
  • . . . . . .

    That worries me; each additional array constitutes an opportunity for something to go wrong.

    Campbell - Thanks . . .

    That's a pleasure
     
    Aria Villano
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What -1 are you referring to? I also did create a for loop to print out the array which is working fine. Also, I thought it would be harder to make the game with 1 array, so I made 2 instead.
     
    Campbell Ritchie
    Marshal
    Posts: 72074
    312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry; I am getting confused about − 1 and I think I am seeing things which aren't there.
     
    Carey Brown
    Bartender
    Posts: 7794
    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
    Aria, I've provided you with several code snippets, and I really think at this point you should show us your code. Is there any reason you hesitate to do so?
     
    Aria Villano
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    No worries. And yes - unfortunately, I cannot share my code online since this is for an assignment. I wish I could because I know a lot of things can be fixed to improve it; plus I have over 100 lines of code anyway.
     
    Campbell Ritchie
    Marshal
    Posts: 72074
    312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    100 lines isn't much, really, but if you aren't allowed to show your code, you aren't allowed to show your code.
     
    Aria Villano
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you guys for your help though
     
    Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic