How do I ignore characters in a string?

Started by Labrasones, August 09, 2011, 17:05:43

Previous topic - Next topic

Labrasones

I'm reading from a text file, then turning the string of numbers into real integer data types. However, for formatting maters and ease of read, I'd like to be able to use spaces in the text file, and then remove them when I read the files, or right there after. I'm sure ther are many methods to do this, what is generally the most used?

Also, I'd like to be able to put commented lines in my text files. so, when I put // , it ignores that line. Once again, I'm sure there are a multitude of ways to acomplish this, but which is the most used?

CodeBunny

Scanners are good for getting integer values, etc.

If you want comments... that's getting more complex.

First off, you need to decide how to divide "sections" of your file. I'd suggest having each line be a separate section. That way, you basically have one scanner finding every line of text, checking to see if it has been commented out (by using the general methods in String), and if it hasn't, creating a new scanner to deal with it.

It all depends on A) how fast you want it to go, B) how in depth you want it to be, and C) whether or not it should be readable (instead of raw bytes). Realize that you can get the fastest speed by creating the file in a program and then saving it to file as raw data, to be read again whenever you need it.

Labrasones

That sounds like a good idea with the scanners. Looks like I'll be doing some research there :P . Alhough, I'm not sure how you can use one to remove a character. Is there any way to like, find and replace all " " with ""? That sounds like a fesable idea to me, but I'm new and I don't know much still.

Meanz

I think the String class provides some replacing functions.
I have honestly never tested them before

String.replace
String.replaceFirst
String.replaceAll

Something like that.

jediTofu

Quote from: Labrasones on August 09, 2011, 20:29:16
That sounds like a good idea with the scanners. Looks like I'll be doing some research there :P . Alhough, I'm not sure how you can use one to remove a character. Is there any way to like, find and replace all " " with ""? That sounds like a fesable idea to me, but I'm new and I don't know much still.

String s;
s = s.replaceAll("\\s+","");
cool story, bro

jediTofu

I didn't read original post...

Maybe something like this?

s = s.replaceAll("(\\s+)|(//.*$)","");

Or maybe...

s = s.replaceAll("(\\s+)|(//.*\r?\n)","");

Just guesses.

PRO TIP:  learn regular expressions, saves lives
cool story, bro

Labrasones

Uhhh, I really don't understand much of your last post jediTofu........ I think I might have skiped a bit in my knowlage. Atempting to teach yourself something like this doesn't always go so well. What do you mean by Regular expressions? Also, I do not understand the "("(\\s+)|(//.*$)","")" . I'm taking a class on introduction to programming this fall. I'm sure that will help me catch up on stuff like this.

jediTofu

http://download.oracle.com/javase/tutorial/essential/regex/

\\s = any white space (tab, newline, etc.)
+ = at least one or more
| = or
(...) = group
. = any char
* = none or more
? = none or one

good luck :)

This regular expression won't work for this text (as it will think everything after "//" is a comment):

dog_path="//poodle"

But if that does not matter, it should be sufficient for what you want.  A pattern matcher using the Scanner class will be faster than reading the entire block of text in and then parsing it, but that shouldn't matter too much.
cool story, bro

Labrasones

Well, that looks like exactly what I was looking for. I just didn't know what it was called. Thanks! That should make reading the text files much easier.