• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Filereader - Reading >1 char at a time

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

- This question relates to using a text file to create a game map.
- My current code reads 1 character at a time.


- Is it possible for the filereader to read 2 characters at once?
- Example -


This way, each "x,y" coordinate (tile) can have more than 1 image at loadup. (using only alpahbet and numerics etc is too limiting)

thankyou

rg
[ March 16, 2008: Message edited by: richard gallagher ]
 
Marshal
Posts: 70308
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by richard gallagher:

thankyou

rg

Don't use == to test Strings for equality; use the String class equals() method.
Use "" round Strings, not ''. That code snippet won't compile because of the ''. You can't get two keystrokes into a char like that.

You can read a whole line with a Buffered Reader, or you can use the Scanner class to read a line. A Scanner will read the next char, int, etc., instead if you ask nicely (nextInt() etc), so lots of people use Scanner for simple text files instead of Readers.

You can split the line into a char[] array with a method of the String class (look through the API; it has an obvious name). You could use the charAt() method of String to find different chars within a String instead.

Don't use a break like that. Declare a String line, thenwill take care of the reading. You need the extra pair of () to force the = operator to be executed before the != because = has a very low precedence.

Your closing the reader leaves you open to resource leaks if there is an exception. You should use something like this which ensures the reader closes even if there is an IOExceptionThe "finally" will make sure to execute that block of code; the != null bit makes sure you only close the reader if it was opened in the first place, and the outer try . . . catch will handle the exceptions.

If you use a Scanner, it consumes Exceptions, but you can get the Exception with its IOException() method.
 
Campbell Ritchie
Marshal
Posts: 70308
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On reading my last post I notice an error; there is a ) missing from the line starting "while ((line = reader.nextLine()." Sorry.
Also the comment about closing the FileReader only applies of you say
 
    Bookmark Topic Watch Topic
  • New Topic