replacing characters or strings from a .dat file
ok ill start off showing my code
//reads city and parses
BufferedReader in = new BufferedReader(new FileReader("miles.dat"));
String city = in.readLine();
// read all the words from the file
System.out.println( "Reading miles.dat..." );
System.out.println();
while( (city = in.readLine()) != null )
{
if(city.startsWith("*") != true) //skip comments
{
if(city.contains(",") == true) //finding words with ","
{
String[] result = (city).split("," + " ");
for (int x=0; x<result.length; x++)
{
System.out.println(result[x]);
}
}
}
}
in.close(); //in closed
what i want to do is split the city string (what i read from the .dat file) at these location s
"," and " " and "[" and "]"
however i cant split it at
"[" or "]"
(because i get an error)
can i replace all the "[" and "]" characters to "," and just split it at the "," or is their an easier way
(the miles.dat file that i am reading from is located at...
http://www.cs.uiowa.edu/~sriram/21/fall07/miles.dat
if your looking at the .dat file i essentially want this
1st line: Youngstown
2nd line: OH
3rd line: [4110,8065]
4th Line: 115436
and so on with the next city
[1579 byte] By [
Mykullski] at [2007-11-20 11:17:59]

# 1 Re: replacing characters or strings from a .dat file
however i cant split it at
"[" or "]"
(because i get an error)
can i replace all the "[" and "]" characters to "," and just split it at the "," or is their an easier wayThe proper way (it may not be easier though) is to find out why you are getting an error. Have you thought to read up on writing regex expressions, if so you may have noticed that the "[" and "]" characters have a special meaning (grouping) in a regex expression so you can't just include them and expect it to work. You need to escape the characters using \\ to match on these characters.
keang at 2007-11-10 2:14:10 >

# 2 Re: replacing characters or strings from a .dat file
You need to escape the characters using \\ to match on these characters.
how do i do that i never heard of escaping characters
# 3 Re: replacing characters or strings from a .dat file
how do i do that i never heard of escaping charactersYou do what I've already suggested - read a tutorial (http://java.sun.com/docs/books/tutorial/essential/regex/literals.html).
Note: This tutorial uses the terms "Quoting" and "metacharacters" to describe how to use characters with special meaning.
keang at 2007-11-10 2:16:17 >

# 4 Re: replacing characters or strings from a .dat file
i dont fully understand the page but how can i split a string by an index of
# 5 Re: replacing characters or strings from a .dat file
Did you really bother to try to understand it, it's not that complicated.
The bit that tells you how to do it is:
There are two ways to force a metacharacter to be treated as an ordinary character:
1. precede the metacharacter with a backslash, or
2. enclose it within \Q (which starts the quote) and \E (which ends it).Which part of this do you not fully understand?
BTW The reason I said to use \\ (2 backslashes) is that in a Java string a single backslash has a special meaning so you need to precede the blashslash with a backslash to get it to be treated as a regular character.
keang at 2007-11-10 2:18:13 >

# 6 Re: replacing characters or strings from a .dat file
i dont understand it one bit i cannot modify the miles.dat file in any way and i dont know how to split it at the [ or ].
im still getting a output of
Youngstown
OH[4110,8065]115436
and not
Youngstown
OH
4110,8065
115436
where do i put the //Q or //E or /Q or /E ?
# 7 Re: replacing characters or strings from a .dat file
oh wow you mean \\ and no // my brains not functioning...
String[] result = (city).split("," + "\\ " + "\\[" + "\\]");
for (int x = 0; x < result.length; x++)
{
System.out.println(result[x]);
}
hmmm why cant i make this split?
# 8 Re: replacing characters or strings from a .dat file
hmmm why cant i make this split?Oh Good Grief... :rolleyes:
Think what you want the split to do - split the string on a ',' OR a '[' OR a ']'. This is an expression using logical operators. Read the documentation on regular expressions (in Pattern (http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html)) and find the logical operator for 'OR'.
Furious activity is no substitute for understanding...
H.H. Williams
dlorde at 2007-11-10 2:21:22 >

# 9 Re: replacing characters or strings from a .dat file
no i need to split it on all of them so it looks like
Youngstown
OH
4110
8065
115436
# 10 Re: replacing characters or strings from a .dat file
thanks guys for the help i got it a different way
for (int i = 0; i < st.countTokens(); i++)
{
cityList[i] = st.nextToken();
System.out.println(cityList[i]);
stateList[i] = st.nextToken().replaceAll(" ", "");
System.out.println(stateList[i]);
longitudeList[i] = st.nextToken();
System.out.println(longitudeList[i]);
latitudeList[i] = st.nextToken();
System.out.println(latitudeList[i]);
populationList[i] = st.nextToken();
System.out.println(populationList[i]);
System.out.println("");
}
# 11 Re: replacing characters or strings from a .dat file
no i need to split it on all of them so it looks likeYeah, sorry, you're right - that's completely different... :rolleyes:
Everything should be made as simple as possible, but not simpler...
A. Einstein
dlorde at 2007-11-10 2:24:22 >

# 12 Re: replacing characters or strings from a .dat file
thanks guys for the help i got it a different wayFair enough, if it works for this problem.
The disadvantage is that it's an unnecessary workaround for a lack of understanding of regular expressions. If this kind of situation arises again, and the workaround isn't suitable, you may find yourself with a more complex regular expression to create, without the experience that would make it easy. It's not a good precedent.
Having said that, you can often program around to avoid using regular expressions, but it's not a good habit to get into. These tools are provided because they are often useful and make life easier - if you take the time to understand how to use them.
The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities...
E. Dijkstra
dlorde at 2007-11-10 2:25:20 >

# 13 Re: replacing characters or strings from a .dat file
so you want your output to printlike this?
city
state
population?
what are those longs for? what does the code look like? i suppose if you've got it figured out it doesn't matter, i'm just curious as to how you coded it.
# 14 Re: replacing characters or strings from a .dat file
what are those longs for? They are distances between cities
what does the code look like? Take my advice, you don't want to know.
i suppose if you've got it figured out it doesn't matter, i'm just curious as to how you coded it. Why do you have the same homework ;)
keang at 2007-11-10 2:27:21 >

# 15 Re: replacing characters or strings from a .dat file
lol no, i just have a similar sounding project, but we are using random numbers (i'm not entirely sure about the whole homework, just was assigned it) but we have to display a graph with random points, then the points have a 90% probability of connecting to points within a set distance, then we have a source point and a final point, and we have to figure out the average number of hops (actually, i think store the hops in an array) and return something...i'm not entirely sure, but it seems quite terrifying.
# 16 Re: replacing characters or strings from a .dat file
Change "terrifying" for "challenging" - there's no point starting the homework thinking you can't do it.
Remember before you attempt to write any code have a really good read through the assignment (in fact, if you have time, it's probably best to read it a few times over a couple of days). It's important not to think about how to solve the problem untill you are sure you fully understand what the problem is, then, using pen and paper, write out how you plan to solve the problem.
And finally if you get stuck start your own thread here clearly explaining the problem you have.
keang at 2007-11-10 2:29:23 >
