formate display
Hi all,
I have data like the follow
1 CAGCAATGGG CTTGAGGATC AGTTCATCTT TTAGTTTTGG AGGCTTCACT
51 TTCAAAAGAA CAAGTGGGTC ATCTGTCAAG AAGGAAGAGG AAGTACTTAC
101 AGGCAACCTC CAAACACTGA AAATAAGAGT ACATGAGGGG TATGAGGAAT .
Is that possible to
display 10 letters(without number) one group, 5 groups one row?
Thank you
[357 byte] By [
TMarya] at [2007-9-30 23:36:04]

not clear with ur requirementcan u b more specific
hi,
You may try this. Hope it helps.
public class DisplayChars {
public static void main(String args[]){
String sampleString = "1 CAGCAATGGG CAGCAATGGG CAGCAATGGG CAGCAATGGG CAGCAATGGG"+
"51 CAGCAATGGG CAGCAATGGG CAGCAATGGG CAGCAATGGG CAGCAATGGG"+
"101 CAGCAATGGG CAGCAATGGG CAGCAATGGG CAGCAATGGG CAGCAATGGG";
int count = 0;
StringBuffer sb = new StringBuffer();
char chars[] = sampleString.toCharArray();
for(int i = 0 ; i<sampleString.length(); i++){
char t = sampleString.charAt(i);
if(!Character.isDigit(t) && t != ' '){ //check if character is a number or a space
if(sb.length() != 10){//checks if a string reaches 10
sb.append(t);
}
else{
count++;
System.out.print(sb.toString()+" ");
sb = new StringBuffer();
sb.append(t);
if(count == 5){//verifies if the row has reached the limit of 5 groups
System.out.print("\n");
count = 0;
}
}
}
}
}
}
>
This seems to be DNA data. Could you elucidate your problem?***Annie***
Hi ann, it is sequence data, I am busy today,and will try your suggestion late.Thank you for your reply.
Hi ann,
it work on java, but if the data come from database how to use string buffer to formate it?
<%
try {
while (rs.next()) {
%>
<table>
<table border="1" width="70%">
<tr>
<Th width="20%">Name</Th>
<td><%= rs.getString(2) %></td></tr>
<tr>
<Th width="20%">Length</Th>
<td><%= rs.getString(3) %>
</td></tr>
<tr>
<Th width="20%">Segment</Th>
<td><%= rs.getString(4) %></td></tr>
<tr>
<Th width="30">Raw Sequence</Th>
<td rowspan="10"><%= rs.getString(5) %>
</td></tr>
<%
}
}
catch (SQLException e) {
// For now, just report the error to the system log
System.out.println(e.toString());
}
%>
</table>
</body>
</html>
I'd recommend that you write a Java class to encapsulate this behavior rather than trying to embed it in ugly scriptlet code in a JSP. It'll be more useful to you in that form, because other apps that don't have access to your JSP will be able to use it, too.
Ever thought about a Chromasome class with a toString() method? You might be able to embed some other useful behavior in that class, too. Much better than working with raw Strings all the time.
%
So,could you please tell me how to put only one column result into String bufferThank you
hi Nanzee,sorry for the mistake, any more suggestion? thank you
hi,
duffymo has a good suggestion that you should write a separate class with a method that would handle your request, call that method via your jsp page. For you to place one column result into a StringBuffer, use the append() method. Example:
while(rs.next()){
StringBuffer sb = new StringBuffer();
sb.append(rs.getString(1)); // i prefer to use column names than column numbers for clarity
...
...
}
StringBuffer class has other methods to manipulate the characters you placed in it. Here's the link : http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StringBuffer.html
> Hi ann,
> it work on java, but if the data come from database
> e how to use string buffer to formate it?
> > <%
> try {
> while (rs.next()) {
> %>
>
>
><table>
><table border="1" width="70%">
><tr>
> <Th width="20%">Name</Th>
> <td><%= rs.getString(2) %></td></tr>
>
> <tr>
><Th width="20%">Length</Th>
><td><%= rs.getString(3) %>
> </td></tr>
>
>
> <tr>
><Th width="20%">Segment</Th>
><td><%= rs.getString(4) %></td></tr>
> <tr>
><Th width="30">Raw Sequence</Th>
><td rowspan="10"><%= rs.getString(5) %>
>
>
>
></td></tr>
>
> <%
> }
> }
> catch (SQLException e) {
> // For now, just report the error to the system
> ystem log
>System.out.println(e.toString());
> }
> %>
> </table>
> </body>
> </html>
This code leads me to believe that your database schema is as awful as the scriptlet code in your JSP. Ever heard of normalization? 3rd normal form?
%
Hi duffymo,
1)I did not pass my select statement, how do you know i did not have join in my select?
2)beside, do you heard about if most of your requirement is select deormalization is better chose? the logical and jsp to display. if you need to cross database and union, normalization let youe select code to long?
people go to this forums when to get some useful advise, please give some detail guide.
3) any way , are you try to tell me use bean to handle it
actually, my display is form html to bean to jsp1to display, yes i guest i need to read more at string buffer.
be nice next time
hi TMary,
I think it's but effective to separate your database transactions outside your jsp for clarity and debug purposes. It isn't a good programming practice to place your sql statements and place all your java codes in your jsp too. Why don't you try to use beans? That's a good start for you. Here's a link that would help you understand it.
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPBeans.html
hi nanzee,
I still need help at understand
StringBuffer sb = new StringBuffer();
char chars[] = sampleString.toCharArray(); how can I set sequences in bean
now in my bean
while (rs.next()) {
setMyNumber(rs.getString("MyNumber"));
setName(rs.getString("Name"));
setSegement(rs.getString("Segment"));
setLength(rs.getString("length"));
StringBuffer sb = new StringBuffer();
sb.append(rs.getString("sequences"));
for( int i=0; i<sequences; i++)
{
if(!i.isDigit(t) && i != ' '){ //check if character is a number or a space
if(i !=10)
line=line+ i; //store letter into line
}//end while
>
may i know what is your expected output for this? can you give an example?
Hi,stilldisplay 10 letters(without number) one group, 5 groups one row?actually, it is not necessary to display sequence data in that kind of order,but, some one have habit to analyze that by eye. so I want the display looks goodfor them.Thank you
Hi,stilldisplay 10 letters(without number) one group, 5 groups one row?actually, it is not necessary to display sequence data in that kind of order,but, some one have habit to analyze that by eye. so I want the display looks goodfor them.Thank you
ok, hope i got you right. So you would want to store the data from your "sequences" column from your database right? And you would want it to be displayed like what you've said. Well, i suggest that you would create a separate method that would handle your "sequences" data.
1. Store this in the class where you have your database transactions. Something like this:
public String storeSequenceData(String sampleString){
int count = 0;
StringBuffer sb = new StringBuffer();
StringBuffer wholeString = new StringBuffer();
char chars[] = sampleString.toCharArray();
for(int i = 0 ; i<sampleString.length(); i++){
char t = sampleString.charAt(i);
if(!Character.isDigit(t) && t != ' '){ //check if character is a number or a space
if(sb.length() != 10){//checks if a string reaches 10
sb.append(t);
}
else{
count++;
wholeString.append(sb.toString());
wholeString.append(" ");
sb = new StringBuffer();
sb.append(t);
if(count == 5){//verifies if the row has reached the limit of 5 groups
wholeString.append("\n");
count = 0;
}
}
}
}
return wholeString.toString();
}
2. I suggest that you would create a separate class say name it "SequenceData", that would contain getters and setters methods for your MyNumber, Name, Segment, length and sequences data. Store this method still on the class where you have your database transaction.
public ArrayList getSequenceDataList(){
//your select statements and other declarations here.....
ArrayList resultsetList = new ArrayList(); //create a list that would contain your results
while (rs.next()) {
SequenceData sequenceData = new SequenceData(); //instantiate an object for this
sequenceData.setMyNumber(rs.getString("MyNumber")); //place the result to this object
sequenceData.setName(rs.getString("Name"));
sequenceData.setSegment(rs.getString("Segment"));
sequenceData.setLength(rs.getString("length"));
sequenceData.setSequences(storeSequenceData(rs.getString("sequences"))); //call the method above that would give you the sequence display data you want
resultsetList.add(sequenceData); //add your object to this list
}
return resultsetList;
//remember to close your resultsets and connection if possible or is not used
}
3. In your jsp page, create a bean for the class containing your database transaction. Call the method getSequenceDataList and store it in a LinkedList. like this:
><%
ArrayList sequenceDataList = yourBeanName.getSequenceDataList();
for(int i=0; i<sequenceDataList.size(); i++){
SequenceData sequenceData = (SequenceData) sequenceDataList.get(i); %> //get the object
<table>
<table border="1" width="70%">
<tr>
<Th width="20%">Name</Th>
<td><%=sequenceData.getName()%></td></tr>
<tr>
<Th width="20%">Length</Th>
<td><%= sequenceData.getLength()%>
</td></tr>
<tr>
<Th width="20%">Segment</Th>
<td><%= sequenceData.getSegment()%></td></tr>
<tr>
<Th width="30">Raw Sequence</Th>
<td rowspan="10"><%=sequenceData.getSequences()%> //here is the data that you want
</td></tr>
<%}%>
ok hope this helps.
> Hi duffymo,
> 1)I did not pass my select statement, how do you know
> i did not have join in my select?
Actually, I never said anything about joins. I only mentioned 3rd normal form.
> 2)beside, do you heard about if most of your
> requirement is select deormalization is better
> chose? the logical and jsp to display. if you need to
> cross database and union, normalization let youe
> select code to long?
I'm willing to be that you're having more problems due to poor design and coding than performance. I'd normalize, add indexes to speed up those queries (a better strategy than denormalization), and then measure to find the true bottleneck if you do indeed have a performance problem.
Yes, I am aware of the fact that denormalization is done. The problem I have is that you've got a query that brings back five sequences in a single row. That suggests a schema like this:
column 1 = number
column 2 = sequence #1
column 3 = sequence #2
column 4 = sequence #3
column 5 = sequence #4
etc.
This violates first normal form, if I understand it correctly. You should have a primary key and one sequence. If a number of sequences go together, then there ought to be a table that brings them together in a JOIN.
> people go to this forums when to get some useful
> l advise, please give some detail guide.
Google for Data Access Object.
> 3) any way , are you try to tell me use bean to
> handle it
> actually, my display is form html to bean to jsp1to
> o display, yes i guest i need to read more at string
> buffer.
No, wrong. You need to get the data into a JavaBean and use JSTL to iterate through a collection to display the table you want dynamically.
Learn to use JSTL. I think I've suggested getting away from that awful scriptlet code several times (you've had many threads), but that doesn't seem to register. You'd be much better off if you did.
> be nice next time
I'm neither nice nor mean. I'm just telling you my opinion. It might say you're not doing it correctly, but that's not necessarily mean. Toughen up.
%
My whole point is that the choice of "five per row" has nothing at all to do with the underlying data and everything to do with your display. You shouldn't build display concerns into your data. You could have easily chosen 20 letter sequences, with six per row, but that would hardly change the DNA you were representing.
Model the data as it is, and keep it separate from the display.
You're not thinking in terms of objects. You're still dealing with these DNA snippets as strings, and collections of them as rows in a display table. Wrong. Better to encapsulate those two abstractions into objects. Then let your display decide how it best wants to display a snippet or chromasome by asking for its data and formatting it.
Decoupling and layering like this will make your development and maintainence tasks much easier.
From the MANY threads you've posted it seems to me that you're thinking about this at too low a level. There's not enough abstraction going on here.
%
Hi Nanzee,Thank you for your replys, I understand way 2 and jsp suggestion, but it don't formate the display . any way thank you for your help.
> Hi Nanzee,
> Thank you for your replys,
> I understand way 2 and jsp suggestion, but it don't
> 't formate the display .
>
> any way thank you for your help.
Hi,
Whatever Nanzee has posted is a good way of doing it. However you will not get it formatted the way you want, because HTML doesn't understand "\n"
Just change the line as follows:
<td rowspan="10"><%=sequenceData.getSequences()%> //here is the data that you want
to
<td rowspan="10"><pre><%=sequenceData.getSequences()%></pre>
//the PRE tag will format it the way you expect.
Just try it and lemme know.
***Annie***
Or just put it in a <table>.%
thanks annie and duffymo.
Hi , Nanzee,
Thank you for you understand my questions
1)Sorry, I did not read your code careful , I try your suggestion, but I got error at
displayBean.java:155: cannot resolve symbol
symbol : class SequenceData
location: class Sec.displayBean
sequenceData = new SequenceData(); //instantiate an object fo
r this
2) I change the code to
public String storeSequenceData(String sampleString){
int count = 0;
StringBuffer sb = new StringBuffer();
StringBuffer wholeString = new StringBuffer();
char chars[] = sampleString.toCharArray();
for(int i = 0 ; i<sampleString.length(); i++){
char t = sampleString.charAt(i);
if(!Character.isDigit(t) && t != ' '){ //check if character is a number or a space
if(sb.length() != 10){//checks if a string reaches 10
sb.append(t);
}
else{
count++;
wholeString.append(sb.toString());
wholeString.append(" ");
sb = new StringBuffer();
sb.append(t);
if(count == 5){//verifies if the row has reached the limit of 5 groups
wholeString.append("\n");
count = 0;
}
}
}
}
return wholeString.toString();
}
public void getSequenceData(){
//your select statements and other declarations here.....
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433","test","1234");
conn.setCatalog("sequences");
Statement s = conn.createStatement();
String Sql=" select NewNum, Name, Length, Segment, Sequences";
Sql+=" from Sec04";
Sql+=" where NewNum='"+click+"'";
Sql+=" union";
Sql+=" select NewNum, Name, Length, Segment, Sequences";
Sql+=" from MySec";
Sql+=" where NewNum='"+click+"'";
rs = s.executeQuery(Sql);
while (rs.next()) {
setNewNum(rs.getString("NewNum")); //place the result to this object
setName(rs.getString("Name"));
setSegment(rs.getString("Segment"));
setLength(rs.getString("length"));
setSequences(storeSequenceData(rs.getString("sequences"))); //call the method above that would give you the sequence display data you want
}
//remember to close your resultsets and connection if possible or is not used
rs=null;
s=null;
conn=null;
}//end try
catch (Exception e) {
System.out.println("Error populating myBean: " + e.toString());
}
}
jsp
><body>
<%@ page import = "Sec.displayBean" %>
<jsp:useBean id="myData" scope="session" class="Sec.displayBean"/>
<jsp:setProperty name="myData" property="*"/>
<basefont face="Arial">
<table border="1" width="70%">
<tr>
<Th width="20%">Name</Th>
<td><jsp:getProperty name="myData" property="name"/></td></tr>
<tr>
<Th width="20%">Length</Th>
<td><jsp:getProperty name="myData" property="length"/>
</td></tr>
<tr>
<Th width="20%">Segment</Th>
<td><jsp:getProperty name="myData" property="segment"/></td></tr>
<tr>
<Th width="30">Raw Sequence</Th>
<td rowspan="10"><jsp:getProperty name="myData" property="sequences"/>
</td></tr>
</table>
I got empty display, why?
> Hi , Nanzee,
> Thank you for you understand my questions
> 1)Sorry, I did not read your code careful , I try
> y your suggestion, but I got error at
> displayBean.java:155: cannot resolve symbol
> symbol : class SequenceData
> location: class Sec.displayBean
1. did you create a class SequenceData?
Make sure you have created that class under your package Sec. A simple class with getters and setters to store your data from your columns.
something like this:
public class SequenceData{
String newNum = null;
String name = null;
String segment = null;
String length = null;
String sequences = null;
public void setNewNum(String x){ newNum = x; }
public void setName(String x){length = x;}
public void setSegment(String x){segment = x;}
public void setLength(String x){length = x;}
public void setSequences(String x){sequences =x;}
public String getNewNum(){return newNum;}
public String getName(){return name;}
public String getSegment(){return segment;}
public String getLength(){return length;}
public String getSequences(){return sequences;}
}
2. Try to follow the previous code again if that works.
Hi, nanzee,
thank you for your kindess, I am surprise i still can get reply on this subject and I don't come to thid form for few days.
right now I can move all space and number away before I store sequences data into database. so on the display part I only need to display 50 letters per line.
few days ago, my code is like
package Sec;
import java.util.*;
import java.sql.*;
import javax.servlet.http.*;
public class displayBean implements java.io.Serializable {
/* Member Variables */
private String NewNum;
private String name;
private String segment;
private String sequences;
private String click;
private String length;
private Connection db = null;
private ResultSet rs=null;
/* Constructor */
public displayBean() {
again();
}
/* Get Database Connection */
private void dbConnect() {
if (db == null) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
db = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;selectMethod=cursor","ADB","@#$");
db.setCatalog("sequences");
}
catch (Exception e) {
System.out.println("Error Connecting to catalog DB: " + e.toString());
}//catch
}//if
}//end connection
/* Accessor Methods */
public String getNewNum() {
return NewNum;
}
/* one way to set data will get from database */
public void setNewNum(String NewNum) {
this.NewNum =NewNum;
}
public String getName() {
return name;
}
public void setName(String _name) {
name = _name;
}
public String getSegment() {
return segment;
}
public void setSegment(String _segment) {
segment = _segment;
}
public String getLength(){
return length;
}
public void setLength( String length ) {
this.length =length;}
public String getClick(){
return click;
}
public void setClick( String click ) {
this.click =click;}
public String getSequences(){
return sequences;
}
public void setSequences( String sequences ) {
this.sequences=sequences;}
public String storeSequenceData(String sampleString){
int count = 0;
StringBuffer sb = new StringBuffer();
StringBuffer wholeString = new StringBuffer();
char chars[] = sampleString.toCharArray();
for(int i = 0 ; i<sampleString.length(); i++){
char t = sampleString.charAt(i);
if(!Character.isDigit(t) && t != ' '){ //check if character is a number or a space
if(sb.length() != 10){//checks if a string reaches 10
sb.append(t);
}
else{
count++;
wholeString.append(sb.toString());
wholeString.append(" ");
sb = new StringBuffer();
sb.append(t);
if(count == 5){//verifies if the row has reached the limit of 5 groups
wholeString.append("\n");
count = 0;
}
}
}
}
return wholeString.toString();
}
public void loadData(){
//your select statements and other declarations here.....
try {
StringSql=" select NewNum, Name, Length, Segment, Sequences";
Sql+=" from Sec04";
Sql+=" where NewNum='"+click+"'";
Sql+=" union";
Sql+=" select NewNum, Name, Length, Segment, Sequences";
Sql+=" from MySec";
Sql+=" where NewNum='"+click+"'";
Statement s = db.createStatement();
rs = s.executeQuery (Sql);
while (rs.next()) {
setName(rs.getString("Name"));
setLength(rs.getString("Length"));
setSegment(rs.getString("Segment"));
setSequences(rs.getString("sequences")); //call the method above that would give you the sequence display data you want
}
//remember to close your resultsets and connection if possible or is not used
rs=null;
s=null;
}//end try
catch (Exception e) {
System.out.println("Error populating myBean: " + e.toString());
}
}
public void again(){
/* Initialize bean properties */
setNewNum("");
setName("");
setSegment("");
setSequences("");
setLength("");
dbConnect();
rs=null;
}
}
>
Hi all,
Thank you for all the help,
I got it work , but the function have problem at
public String storeSequenceData(String Sequence){
int count = 0;
StringBuffer sb = new StringBuffer();
StringBuffer wholeString = new StringBuffer();
char chars[] = Sequence.toCharArray();
for(int i = 0 ; i<Sequence.length(); i++){
char t = Sequence.charAt(i);
if(!Character.isDigit(t) && t != ' '){ //check if character is a number or a space
if(sb.length() !=10){//checks if a string reaches 10
sb.append(t);
}
else{
count++;
wholeString.append(sb.toString() + " ");
sb = new StringBuffer();
sb.append(t);
if(count == 5){//verifies if the row has reached the limit of 5 groups
wholeString.append("\n");
count = 0;
}
}
}
}
return wholeString.toString();
}
will cut the sequence letters which is less then 10, what can i do ?
Thank you>
Hi Mary,Back after a long time... happy new year.What you need to add before the return statement is:wholeString.append(sb);This will get the remaining portion.Cheers!***Annie***
public String storeSequenceData(String sequence){
int count = 0;
StringBuffer sb = new StringBuffer();
StringBuffer wholeString = new StringBuffer();
char chars[] = sequence.toCharArray();
for(int i = 0 ; i<sequence.length(); i++){
char t = sequence.charAt(i);
//check if character is a number or a space
if(!Character.isDigit(t) && t!=' ') {
if(sb.length() != 10){ //checks if a string reaches 10
// System.out.println("><1>" +t);
sb.append(t);
} else{
// System.out.println("<2>" +t);
wholeString.append(sb).append(" ");
sb = new StringBuffer();
count++;// Increment count of a group
if((count != 0) && (count%5 == 0)) {
wholeString.append("\n");
}
sb.append(t);
}
}
}
wholeString.append(sb);
return wholeString.toString();
}
Mary, I made a slight modification to your code. This will get the required display format.
Cheers!
***Annie***