As part of my Object Oriented class, we are to design a Battleship game which uses a TUI to eventually implement a GUI. According to the design, we are to read the AI's ship locations from a text file that will look similar to the one b
ABCDEFGHIJ
1
2 BBBB
3
4 C
5D C
6D
7AAAAA
8 SSS
9
0
Where the letters represent different ships. My current implementation of the game uses a 2 dimensional array of characters, so I would like to be able to read the text file and create the corresponding 2D array. I've seen a few built in functions that allow you to read the next String or the next Integer, but I just want to read it character by character. Is there a way to do this? Thanks in advance!
You can use a
Scanner
to read an individual character like so:The board is an 11x11 of characters (
char[][] board = new char[11][11]
), so you'll have to track what row and column you're on as you're reading the characters. You'll know when to go to the next row after you've read the 11th character.Code Sample:
Results:
Have a look here.You can read character by character in this way. You need to aware of the new lines. http://docs.oracle.com/javase/tutorial/essential/io/charstreams.html
The general idea is to break the problem into simpler to solve problems. If you have a function that can open a file and read a single line, then you solved one problem. Next you need to solve the problem of iterating through a string character by character. This can be done multiple ways, but one way is to use the built in functions of the String class to convert the String to a character array.
Only thing missing is the try{} catch{} usually required when opening a file along with correctly importing the classes: FileReader & BufferedReader.
The simplest way to do this, in my opinion, is to first read the file line-by-line then read those lines character-by-character. By using a built-in utility, you can avoid the complexities of handling new lines because they vary by OS/Editor.
BufferedReader Docs
@JudgeJohn's answer is good - I would comment but I can't unfortunately. Reading first line by line will ensure that any system details like implementation of line breaks are handled by Java's various libraries, which know how to do that very well. Using a
Scanner
on some kind of reader over a file will allow easy enumeration over lines. After this, reading the obtainedString
character by character should not be a problem, for example using thetoCharArray()
method.An important addition - when using
Stream
andReader
objects, andScanner
s over them, it's very often important that your code handles the end of the process well - disposing of system resources like file handles. This is done in Java using theclose()
methods of these classes. Now, if we finish reading and just callclose()
, this is all very well when things go to plan, but it's possible that Exceptions could be thrown, causing the method to exit before theclose()
method is called. A good solution is atry - catch
ortry - finally
block. E.g.Even better is
The
finally
block always gets called, regardless of how thetry
block exits. Even better, there's a try-with-resources block in Java, which goes like this:This one does all the checking for nulls,
finally
and calling ofclose()
. Very safe, and quick to type!