Hello Guest

How do I ignore characters in a string?

  • 8 Replies
  • 10473 Views
How do I ignore characters in a string?
« on: August 09, 2011, 17:05:43 »
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?

Re: How do I ignore characters in a string?
« Reply #1 on: August 09, 2011, 17:25:42 »
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.

Re: How do I ignore characters in a string?
« Reply #2 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.
« Last Edit: August 10, 2011, 14:45:15 by Labrasones »

*

Offline Meanz

  • *
  • 40
Re: How do I ignore characters in a string?
« Reply #3 on: August 11, 2011, 10:16:00 »
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.

Re: How do I ignore characters in a string?
« Reply #4 on: August 12, 2011, 04:11:06 »
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

Re: How do I ignore characters in a string?
« Reply #5 on: August 12, 2011, 04:15:12 »
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

Re: How do I ignore characters in a string?
« Reply #6 on: August 15, 2011, 00:36:25 »
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.

Re: How do I ignore characters in a string?
« Reply #7 on: August 15, 2011, 04:48:27 »
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

Re: How do I ignore characters in a string?
« Reply #8 on: August 15, 2011, 14:36:57 »
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.