mindweaver
asked on
StringTokenizer working on plain String text but not on String from MS Access (text = rs.getString(1);)
Hey,
I have a problem using a string, that I have read from a MS Access-database, in a StringTokenizer. I have to user the StringTokenizer to recognize \n so that I can put all separate lines into an array that I will later paint with a drawString()
Below is an working example where I get the text from a simple String text:
(I have declared "String text;" in my class far up in the code)
text = "this is row 1\nthis is row 2\nthis is row 3";
StringTokenizer st = new StringTokenizer(text,"\n") ;
num_lines = st.countTokens();
lines = new String[num_lines];
line_widths = new int[num_lines];
for (int i = 0; i < num_lines; i++){
lines[i] = st.nextToken();
This works like a charm and I have an array with the length of 3.
Below is the example I want to get working and that just wont work:
try {
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver"); // Calling the JDBC:ODBC bridge to be able to connect to our MS Access database
String filename = "c:/jollydb.mdb"; // declaring and setting the database-file variable
String database = "jdbc:odbc:Driver={Microso ft Access Driver (*.mdb)};DBQ="; // declaring and assigning the database driver variable
database+= filename.trim() + ";DriverID=22;READONLY=tru e}";
Connection con = DriverManager.getConnectio n( database ,"",""); // creating a connection object to communicate with the database via DriverManager
Statement s = con.createStatement(); // creating a java.sql.Statement
s.execute("SELECT news_text FROM JT_content"); // getting the our text from the database
ResultSet rs = s.getResultSet(); // get any ResultSet that came from our query
while ( rs.next() ) { // stepping through our data row-by-row
text = rs.getString(1); // setting the text to be scrolled
}
s.close(); // closing the Statement
con.close(); // closing the Connection
}
catch (Exception e) {
System.out.println("Error: " + e);
}
StringTokenizer st = new StringTokenizer(text,"\n") ;
num_lines = st.countTokens();
lines = new String[num_lines];
line_widths = new int[num_lines];
for (int i = 0; i < num_lines; i++){
lines[i] = st.nextToken();
The output of this will be "this is row 1\nthis is row 2\nthis is row 3", meaning that it doesn't do **** with my "text"-string.
What am I doing wrong? I have tried to do text.toString(); on it but it doesn't help.
Would love a quick solution.
I have a problem using a string, that I have read from a MS Access-database, in a StringTokenizer. I have to user the StringTokenizer to recognize \n so that I can put all separate lines into an array that I will later paint with a drawString()
Below is an working example where I get the text from a simple String text:
(I have declared "String text;" in my class far up in the code)
text = "this is row 1\nthis is row 2\nthis is row 3";
StringTokenizer st = new StringTokenizer(text,"\n")
num_lines = st.countTokens();
lines = new String[num_lines];
line_widths = new int[num_lines];
for (int i = 0; i < num_lines; i++){
lines[i] = st.nextToken();
This works like a charm and I have an array with the length of 3.
Below is the example I want to get working and that just wont work:
try {
Class.forName("sun.jdbc.od
String filename = "c:/jollydb.mdb"; // declaring and setting the database-file variable
String database = "jdbc:odbc:Driver={Microso
database+= filename.trim() + ";DriverID=22;READONLY=tru
Connection con = DriverManager.getConnectio
Statement s = con.createStatement(); // creating a java.sql.Statement
s.execute("SELECT news_text FROM JT_content"); // getting the our text from the database
ResultSet rs = s.getResultSet(); // get any ResultSet that came from our query
while ( rs.next() ) { // stepping through our data row-by-row
text = rs.getString(1); // setting the text to be scrolled
}
s.close(); // closing the Statement
con.close(); // closing the Connection
}
catch (Exception e) {
System.out.println("Error:
}
StringTokenizer st = new StringTokenizer(text,"\n")
num_lines = st.countTokens();
lines = new String[num_lines];
line_widths = new int[num_lines];
for (int i = 0; i < num_lines; i++){
lines[i] = st.nextToken();
The output of this will be "this is row 1\nthis is row 2\nthis is row 3", meaning that it doesn't do **** with my "text"-string.
What am I doing wrong? I have tried to do text.toString(); on it but it doesn't help.
Would love a quick solution.
ASKER
zzynx
Thanks for your quick reply, but no it had no effect.
Thanks for your quick reply, but no it had no effect.
Hey, think that should be:
StringTokenizer st = new StringTokenizer(text,"\\n" );
StringTokenizer st = new StringTokenizer(text,"\\n"
And if that doesn't work:
StringTokenizer st = new StringTokenizer(text,"[\\n |\\r|\\n\\ r|\\r\\n]" );
StringTokenizer st = new StringTokenizer(text,"[\\n
I read for the StringTokenizer constructor with only one parameter:
Constructs a string tokenizer for the specified string.
The tokenizer uses the default delimiter set, which is " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens.
So also try:
StringTokenizer st = new StringTokenizer(text);
Constructs a string tokenizer for the specified string.
The tokenizer uses the default delimiter set, which is " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens.
So also try:
StringTokenizer st = new StringTokenizer(text);
mindweaver, sorry the StringTokenizer doesn't take regular expression.
It's the String.split() that does.
So if the previous doesn't work
Try:
String lines[] = text.split("\\n");
or
String lines[] = text.split(""[\\n|\\r|\\n\ \r|\\r\\n] ");
It's the String.split() that does.
So if the previous doesn't work
Try:
String lines[] = text.split("\\n");
or
String lines[] = text.split(""[\\n|\\r|\\n\
ASKER
Thanks alot!
StringTokenizer st = new StringTokenizer(text,"\\n" );
worked like a charm!
StringTokenizer st = new StringTokenizer(text,"\\n"
worked like a charm!
Thanks
So, with
StringTokenizer st = new StringTokenizer(text,"\n") ;
you were searching for a sequence of
1) a back slash followed by
2) the character 'n'
instead of a newline.
I would really try
String lines[] = text.split("\\n");
So, with
StringTokenizer st = new StringTokenizer(text,"\n")
you were searching for a sequence of
1) a back slash followed by
2) the character 'n'
instead of a newline.
I would really try
String lines[] = text.split("\\n");
That one line of code replaces all these
StringTokenizer st = new StringTokenizer(text,"\\n" );
num_lines = st.countTokens();
lines = new String[num_lines];
line_widths = new int[num_lines];
for (int i = 0; i < num_lines; i++)
lines[i] = st.nextToken();
StringTokenizer st = new StringTokenizer(text,"\\n"
num_lines = st.countTokens();
lines = new String[num_lines];
line_widths = new int[num_lines];
for (int i = 0; i < num_lines; i++)
lines[i] = st.nextToken();
ASKER
Yeah I was looking for a backslash followed by the character n. I had manually put those \n into the text in the database field.
ASKER
too bad I can't use that oneliner you gave me, would be nice to get rid of all that code I put in and replace it with yours.
>>too bad I can't use that oneliner you gave me
Why nt?
Why nt?
ASKER
I tried it, I got no output. and isn't it looking for a "real" new line? There are no real line breaks in the db field (don't think it's possible in ms access) it's only a text string "this is row 1\nthis is row 2\nthis is row 3" in the db field.
>> There are no real line breaks in the db field
Oh, I see. Then you should try:
String lines[] = text.split("\n");
Oh, I see. Then you should try:
String lines[] = text.split("\n");
ASKER
Nope, tried it, gave me no output.
Code:
try {
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver"); // Calling the JDBC:ODBC bridge to be able to connect to our MS Access database
String filename = "c:/jollydb.mdb"; // declaring and setting the database-file variable
String database = "jdbc:odbc:Driver={Microso ft Access Driver (*.mdb)};DBQ="; // declaring and assigning the database driver variable
database+= filename.trim() + ";DriverID=22;READONLY=tru e}";
Connection con = DriverManager.getConnectio n( database ,"",""); // creating a connection object to communicate with the database via DriverManager
Statement s = con.createStatement(); // creating a java.sql.Statement
s.execute("SELECT news_text FROM JT_content"); // getting the our text from the database
ResultSet rs = s.getResultSet(); // get any ResultSet that came from our query
while ( rs.next() ) { // stepping through our data row-by-row
text = rs.getString(1); // setting the text to be scrolled
}
s.close(); // closing the Statement
con.close(); // closing the Connection
}
catch (Exception e) {
System.out.println("Error: " + e);
}
String lines[] = text.split("\n");
Font f = getFont(); // getting our font context
FontMetrics fm = getFontMetrics(f); // gettomg out FontMetrics
line_height = fm.getHeight(); // setting the line_height variable to the our font lineheight
for (int i=0; i<num_lines; i++){ // creating a loop for drawing all of our lines
g.drawString(lines[i], x, flexibleHeight + (i * line_height)); // drawing one line from the lines-array
}
Code:
try {
Class.forName("sun.jdbc.od
String filename = "c:/jollydb.mdb"; // declaring and setting the database-file variable
String database = "jdbc:odbc:Driver={Microso
database+= filename.trim() + ";DriverID=22;READONLY=tru
Connection con = DriverManager.getConnectio
Statement s = con.createStatement(); // creating a java.sql.Statement
s.execute("SELECT news_text FROM JT_content"); // getting the our text from the database
ResultSet rs = s.getResultSet(); // get any ResultSet that came from our query
while ( rs.next() ) { // stepping through our data row-by-row
text = rs.getString(1); // setting the text to be scrolled
}
s.close(); // closing the Statement
con.close(); // closing the Connection
}
catch (Exception e) {
System.out.println("Error:
}
String lines[] = text.split("\n");
Font f = getFont(); // getting our font context
FontMetrics fm = getFontMetrics(f); // gettomg out FontMetrics
line_height = fm.getHeight(); // setting the line_height variable to the our font lineheight
for (int i=0; i<num_lines; i++){ // creating a loop for drawing all of our lines
g.drawString(lines[i], x, flexibleHeight + (i * line_height)); // drawing one line from the lines-array
}
Shouldn't
for (int i=0; i<num_lines; i++){
be
for (int i=0; i<lines.length; i++){
?
for (int i=0; i<num_lines; i++){
be
for (int i=0; i<lines.length; i++){
?
ASKER
Yeah :) damn have been looking at this code for 6 hours..
Thanks, still having problems though. Now I get an output but it gives the the whole string on one row :
"this is row 1\nthis is row 2\nthis is row 3"
tried both
String lines[] = text.split("\n");
and
String lines[] = text.split("\\n");
Damn, I wish I could give you more points for helping me.
Thanks, still having problems though. Now I get an output but it gives the the whole string on one row :
"this is row 1\nthis is row 2\nthis is row 3"
tried both
String lines[] = text.split("\n");
and
String lines[] = text.split("\\n");
Damn, I wish I could give you more points for helping me.
Well, for me it works:
String inputString = "this is row 1\nthis is row 2\nthis is row 3";
String lines[] = inputString.split("\n");
System.out.println(lines.l ength); // <<<<<< this nicely prints "3"
Could you insert that line
System.out.println(lines.l ength);
in your code and tell the result?
>>Damn, I wish I could give you more points for helping me.
You're allowed to give 500 points per question.
Ask for reopening this one, incrementing the points and reaccept is an option.
;°)
String inputString = "this is row 1\nthis is row 2\nthis is row 3";
String lines[] = inputString.split("\n");
System.out.println(lines.l
Could you insert that line
System.out.println(lines.l
in your code and tell the result?
>>Damn, I wish I could give you more points for helping me.
You're allowed to give 500 points per question.
Ask for reopening this one, incrementing the points and reaccept is an option.
;°)
ASKER
Yeah well that's the problem :) We are back at square one.
String inputString = "this is row 1\nthis is row 2\nthis is row 3";
You got it from a nice little text-string you made static in the code.
when I do that it works for me aswell, it is when I get the exact same
string from my database that it won't recognize my "\n"'s
I have no idea why and it is really frustrating:
text = rs.getString(1); // setting the text to be scrolled
__
String inputString = "this is row 1\nthis is row 2\nthis is row 3"; (lines.length = 3)
same string but in:
text = rs.getString(1); // setting the text to be scrolled (lines.length = 1)
String inputString = "this is row 1\nthis is row 2\nthis is row 3";
You got it from a nice little text-string you made static in the code.
when I do that it works for me aswell, it is when I get the exact same
string from my database that it won't recognize my "\n"'s
I have no idea why and it is really frustrating:
text = rs.getString(1); // setting the text to be scrolled
__
String inputString = "this is row 1\nthis is row 2\nthis is row 3"; (lines.length = 3)
same string but in:
text = rs.getString(1); // setting the text to be scrolled (lines.length = 1)
Well, I would print out that "text" variable character by character (print out the ascii code) to see what Access made from that '\'+'n'
Once you know that you can pass it in split()
Once you know that you can pass it in split()
ASKER
Me too if I knew how to do it :)
String inputString = "A\nB\nC";
for (int i=0; i<inputString.length(); i++)
System.out.println(""+ inputString.charAt(i) + " - " +(int)inputString.charAt(i ));
Prints out:
A - 65
// <<<<<< here the newline is printed out
- 10 // <<<<<< ASCII is indeed 10 (LineFeed)
B - 66
- 10
C - 67
for (int i=0; i<inputString.length(); i++)
System.out.println(""+ inputString.charAt(i) + " - " +(int)inputString.charAt(i
Prints out:
A - 65
// <<<<<< here the newline is printed out
- 10 // <<<<<< ASCII is indeed 10 (LineFeed)
B - 66
- 10
C - 67
Try it with putting "A\nB" in the database
ASKER
it gave me
A - 65
\ - 92
n - 110
B - 66
Have tried to read up on how to put it into the split()-thingie but can't figure it out.
A - 65
\ - 92
n - 110
B - 66
Have tried to read up on how to put it into the split()-thingie but can't figure it out.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
At last ;°)
If you want to reopen this question, post a zero-point question in https://www.experts-exchange.com/Community_Support/
Subject: Moderator Please Reopen
Body: Please reopen this question:
https://www.experts-exchange.com/questions/21159428/StringTokenizer-working-on-plain-String-text-but-not-on-String-from-MS-Access-text-rs-getString-1.html
If you want to reopen this question, post a zero-point question in https://www.experts-exchange.com/Community_Support/
Subject: Moderator Please Reopen
Body: Please reopen this question:
https://www.experts-exchange.com/questions/21159428/StringTokenizer-working-on-plain-String-text-but-not-on-String-from-MS-Access-text-rs-getString-1.html
ASKER
awesome :) it worked!
thanks ALOT! :)
thanks ALOT! :)
:)
Hi mindweaver,
ee_ai_construct tells us that this Q has been reopened.
But I didn't receive a new "Good Answer" mail, so I think it wasn't really reopened, right?
Or did I miss that mail and have you forgotten to increment the points before re-accepting?
ee_ai_construct tells us that this Q has been reopened.
But I didn't receive a new "Good Answer" mail, so I think it wasn't really reopened, right?
Or did I miss that mail and have you forgotten to increment the points before re-accepting?
Hi mindweaver,
Could you please make sure you
- *first* increment the points
- *then* re-accept the comment
(See the EE help pages if you don't know how to increment the points. Or ask here)
Could you please make sure you
- *first* increment the points
- *then* re-accept the comment
(See the EE help pages if you don't know how to increment the points. Or ask here)
Thanks mindweaver
StringTokenizer st = new StringTokenizer(text,"[\n|