Organising data from a BufferedReader

Taking out 'chunks' of data from a file

Page 1 of 1

9 Replies - 1685 Views - Last Post: 27 December 2010 - 03:51 AM Rate Topic: -----

#1 nicholas_white  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-December 10

Organising data from a BufferedReader

Posted 24 December 2010 - 02:40 PM

Hi folks,

First of all I'd like to say hi and I hope you are all having a wonderful time during the festive period :)

Right, being the strange individual I am I thought I'd partake in some Christmas eve programming. I'm relatively/extremely new to programming (started both Java and C# towards the end of September) so my terminology might not be correct, but please bear with me.

Essentially I'm trying to figure out how to separate the data pulled from the file into separate chunks for further processing, alas, I'm struggling to grasp it and I wondered if anyone could offer any enlightenment.

Current code so far:

import java.io.*;

public class reader {
	
	public static void main(String[] args) throws IOException {
		
		// Variables
		int readFile = 0;
		char castChar;
		
		// BufferedReader stream and stuff
		BufferedReader in = new BufferedReader( new BufferedReader (new FileReader("data.txt")) );
		
		// Garbage start
		readFile = in.read();
		
		// Casting decimals to character
		castChar = (char) readFile;
		
		// Dealing with the stream
		while (readFile != -1)
		{
			
		}
	    	    
		// Garbage out
		in.close();
	}
}



Ignoring the cast at the moment, I thought it would be easier to deal with the stream in decimal forms?

Contents of data.txt:

x=8
y=6

12111111
10000101
11010001
10011101
10100001
13101111



The chuck of seemingly random digits at the bottom will later be stored into a 2D array for collision detection, but baby steps.

A gentle nudge in the right direction would be greatly appreciated, I had thought of using the mark() method but again, I'm a bit clueless.

Thank you,

Nick

Is This A Good Question/Topic? 0
  • +

Replies To: Organising data from a BufferedReader

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10365
  • View blog
  • Posts: 38,396
  • Joined: 27-December 08

Re: Organising data from a BufferedReader

Posted 24 December 2010 - 03:31 PM

The easiest way to do this would be to store the lines in an ArrayList<String> or ArrayList<Integer>, and process them from there.
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2646
  • View blog
  • Posts: 11,167
  • Joined: 20-September 08

Re: Organising data from a BufferedReader

Posted 24 December 2010 - 06:13 PM

Try something like

        BufferedReader in = new BufferedReader(new BufferedReader(
                    new FileReader("data.txt")));
        int x = Integer.valueOf(in.readLine().split("=")[1]);
        int y = Integer.valueOf(in.readLine().split("=")[1]);
        in.readLine(); // read blank line (better to get rid of it altogether)

        int[][] matrix = new int[x][y];

        for (int i = 0; i < y; i++) {
            String digits = in.readLine();

            for (int j = 0; j < x; j++) {
                matrix[i][j] = digits.charAt(j) - '0';
            }
        }


Was This Post Helpful? 1
  • +
  • -

#4 Jimmy.Li  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 57
  • Joined: 12-October 10

Re: Organising data from a BufferedReader

Posted 26 December 2010 - 01:14 PM

Another way to do this is the Scanner class, which provides you with the ability to read pretty much any primitive data type and entire lines with it's many methods. It also supports Regular Expressions(Regex), which is a bit complicated but very useful in String manipulation. Scanner also buffers all the data and takes care of all the streaming issues.
Java Doc On Scanner
Regular Expressions
Scanner in=new Scanner(new FileInputStream(fileName));
//Read the X and Y dimensions.
int w=new Integer(in.findInLine("\d+")),
h=x=new Integer(in.findInLine("\d+"));

//Create storage space.
int[][] data=new int[w][h];
//Read the data block using the same method.
[quote]
for (int i = 0; i < y; i++) {
	String digets = in.readLine();
	for (int j = 0; j < x; j++) {
		matrix[i][j] = digits.charAt(j) - '0';	    
	}
}
[/quote]


Was This Post Helpful? 1
  • +
  • -

#5 nicholas_white  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-December 10

Re: Organising data from a BufferedReader

Posted 26 December 2010 - 01:50 PM

Thank you very much folks, all the information is greatly appreciated - thank you!

Since you get nowhere by just copy/pasting code I'm currently in the process of breaking it down to see how exactly it works and to understand it.

So the scanner class is easier to implement than a bufferedreader? Looking over it and reading into the Java doc at the moment.

@Jimmy.Li - Perusing over your code at the moment as well but Eclipse annotates the escape sequence for 'w' as invalid?

*continues researching*
Was This Post Helpful? 0
  • +
  • -

#6 Jimmy.Li  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 57
  • Joined: 12-October 10

Re: Organising data from a BufferedReader

Posted 26 December 2010 - 02:29 PM

Scanner provides String manipulation capabilities, which means a lot less parsing and checking for errors. There are many cases when simply reading in lines(Using Reader/Writer Classes or java.nio(New IO)) or reading in bytes(Stream classes or java.io(Classic IO)) is more efficient (as Scanner does need to buffer in order implement it's methods), such as when each line needs to be simply stored.
As to the Eclipse statement, I am not sure. I have never used Eclipse before, but I'm certain "\w" is a valid Regular Expression escape sequence though not a regular string escape sequence). For example.
import java.util.regex.*;
...
//This will match a word or digets.
Matcher m = Pattern.compile("\w+").matcher("");
m.reset("Data String");
while( m.find() ){
	String word=m.group();
}


Regex Sequence Table

This post has been edited by Jimmy.Li: 26 December 2010 - 02:58 PM

Was This Post Helpful? 0
  • +
  • -

#7 Jimmy.Li  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 57
  • Joined: 12-October 10

Re: Organising data from a BufferedReader

Posted 26 December 2010 - 03:02 PM

By the way, not all methods in Java take in and understand Regex Strings, so if you try to output something like "\w is a character", it will fail. :bigsmile: (Although that would certainly be nice)
Was This Post Helpful? 0
  • +
  • -

#8 indexoutofbounds  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 26-December 10

Re: Organising data from a BufferedReader

Posted 26 December 2010 - 05:04 PM

View PostJimmy.Li, on 26 December 2010 - 02:02 PM, said:

By the way, not all methods in Java take in and understand Regex Strings, so if you try to output something like "\w is a character", it will fail. :bigsmile: (Although that would certainly be nice)


\w in regex is always a character. Here (Pattern.compile(regex)) is the regex engine that is working.

and probably the best way is to separate in array lists as macosxnerd101 said :


	private static  List<Integer>   numericList      = new ArrayList<Integer>();
	private static  List<Character> characterList    = new ArrayList<Character>();
	private static  List<String>    otherSymbolsList = new ArrayList<String>();
	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException, FileNotFoundException{
		BufferedReader in    = new BufferedReader(new FileReader("data.txt"));
		String         line  = "";
		char[]         chars = null;
		while((line = in.readLine()) != null){
			chars = line.trim().toCharArray();
			filterData(chars);
		}
		in.close();
	}
	
	private static void filterData(char[] chars){
		char    currentCharacter = ' ';
		for(int i = 0; i<chars.length; i++){
			currentCharacter = chars[i];
			if(Character.isLetter(currentCharacter)){
				characterList.add(currentCharacter);
			}else if(Character.isDigit(currentCharacter)){
				numericList.add(Character.getNumericValue(currentCharacter));
			}else{
				otherSymbolsList.add(Character.valueOf(currentCharacter).toString());
			}
		}
		
	}



with Character wrapper class you can do most checks
I did not tested this but is something like this if you want to filter data :)
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10365
  • View blog
  • Posts: 38,396
  • Joined: 27-December 08

Re: Organising data from a BufferedReader

Posted 26 December 2010 - 05:08 PM

I didn't mean to use separate ArrayLists. I know there are lines of only numbers, so I was saying store those in a List<Integer> by parsing them to ints when read in or a List<String> to be converted later.

I strongly advocate against the practice of using parallel arrays or Collections as you described. See my tutorial Moving Away From Parallel Arrays for more information. :)
Was This Post Helpful? 0
  • +
  • -

#10 indexoutofbounds  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 26-December 10

Re: Organising data from a BufferedReader

Posted 27 December 2010 - 03:51 AM

View Postmacosxnerd101, on 26 December 2010 - 04:08 PM, said:

I didn't mean to use separate ArrayLists. I know there are lines of only numbers, so I was saying store those in a List<Integer> by parsing them to ints when read in or a List<String> to be converted later.

I strongly advocate against the practice of using parallel arrays or Collections as you described. See my tutorial Moving Away From Parallel Arrays for more information. :)

Ah, ok macosxnerd101, thanks for the advice :clap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1