aspose file tools
The moose likes Beginning Java and the fly likes Using BufferedReader to read numbers larger than 9 (several place values) into 2D array. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Using BufferedReader to read numbers larger than 9 (several place values) into 2D array." Watch "Using BufferedReader to read numbers larger than 9 (several place values) into 2D array." New topic
Author

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

Jai Gates
Greenhorn

Joined: Jul 11, 2012
Posts: 18
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jul 11, 2012
Posts: 18
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
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
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

Joined: Jul 11, 2012
Posts: 18
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!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Using BufferedReader to read numbers larger than 9 (several place values) into 2D array.
 
Similar Threads
Pass file into 2d array
Read data from a file into a 2D array
using BufferedReader and looping
Incompatible type issue
Graphics 2d Object and Scroll Bar Issue