• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using BufferedReader to read numbers larger than 9 (several place values) into 2D array.  RSS feed

 
Jai Gates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey All,

I'm trying to figure out how BufferedReader works. The best idea I've come up with to read chars from text such as: 128, 23 , 10,..

is to read the line in as a String using 1d array for each line like so
.


I guess my questions are:

1. Will I have to use the BufferedReader-based control structure while(in.read()! = -1) or I will encounter issues? I have to ultimately load a 2D array, and don't know how to rig it if so.
2. If I am weeding out that which is not a number, is there a better method than .split(" ") bc what if its more than a space the size of " " ? Commas, larger spaces.... ( and the read() option only pulls characters in one at a time!)

-J
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're reading a text file, it's easier to use BufferedReader.readLine() and keep looping until it returns null.

As for splitting on more than just a single space, you need to determine what your rules are for the format of the file. If you're free to define them yourself, you can make the coding a little simpler by requiring exactly one space between numbers. But if someone else is defining the format, or if you want to be a bit more flexible, it's a pretty simple to create a regex for, say, "1 or more spaces" or "1 comma, possibly preceded and or followed by spaces". It all starts with clearly, precisely, and completely defining the format though.
 
Jai Gates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

Yeah I need to learn start learning regex. I opted to use String[]arr = string.split(" ") . Evidently it removes all the spaces in a string that has some characters and before loading into String array.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MyString.split(" "); will split only on space characters, not finding tabs etc. If you have double spaces, it will find an empty String between the spaces. You are probably better off splitting on "\\s+". That means any non‑zero number of whitespace characters. More details in this tutorial maybe in the predefined character classes section. there are many other regular expression tutorials.
 
Jai Gates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have double spaces, it will find an empty String between the spaces.



So are you saying that it will load an array with empty strings?


I got a strange output is why I ask. When I did a I did a System.out.print

of String.split(" ") INPUT:

OUTPUT 10342910001



If I did System.out.println of String.split[" "]

INPUT:

OUTPUT (note spaces retained)

10

34

29



1000

1

Interesting but not much time to investigate. Will try regex \\s+ thanks!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!