Home arrow static arrow Java Programming [Archive] - Comparing characters in Java
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Comparing characters in Java
This topic has 11 replies on 1 page.

Posts:12
Registered: 8/7/04
Comparing characters in Java  
Aug 7, 2004 10:36 AM



 
Hi,
I have an assignment due that involves comparing sequences of strings. The file would look as follows:
Ex1
AAAAACCCCGGGGTTTTT
GGGGTTTTCACACACTAT
AGAGTTTCCCCAAGGAAG
Ex2

">Ex1" is the name of the sequence, and what follows is the sequence, broken up into lines. I need to concatenate the 3 lines of Ex1. I was thinking of reading each line, adding this to a variable, until I reach the next ">". My problem is the syntax. How do I say:
while (the first character of the line DOES NOT EQUAL ">")?
Would I use charAt and !=? Or should I substring and use !=? I can't seem to find the correct syntax to express this. I would appreciate any help out there. Thank you.
 

Posts:18,384
Registered: 21.03.00
Re: Comparing characters in Java  
Aug 7, 2004 10:40 AM (reply 1 of 11)



 
Hi,

Yes you should use charAt(0) and != '>'

/Kaj
 

Posts:12
Registered: 8/7/04
Re: Comparing characters in Java  
Aug 7, 2004 10:42 AM (reply 2 of 11)



 
Thanks, but would you write it as:
while (line.charAt(0) != ">")?
 

Posts:18,384
Registered: 21.03.00
Re: Comparing characters in Java  
Aug 7, 2004 10:44 AM (reply 3 of 11)



 
Hi,

Yes, but replace " with ' since you are comparing characters.

/Kaj
 

Posts:8,813
Registered: 10/4/00
Re: Comparing characters in Java  
Aug 7, 2004 10:55 AM (reply 4 of 11)



 
I haven't tested this, but it should give you the idea.
import java.io.*;import java.util.ArrayList;public class Test3 {  public static void main(String[] args) {    ArrayList al = new ArrayList();    try {      BufferedReader br = new BufferedReader(new FileReader("data.dat"));      String line,name=null;      StringBuffer sb = new StringBuffer();      while ((line=br.readLine())!=null) {        if (line.charAt(0)=='>') {          if (name!=null && sb.length()>0) al.add(new Thingy(name,sb.toString()));          name = line.substring(1);          sb.setLength(0);        } else {          sb.append(line);        }      }      if (name!=null && sb.length()>0) al.add(new Thingy(name,sb.toString()));    }    catch (FileNotFoundException fnfe) { System.out.println("FNF"); }    catch (IOException ioe) { ioe.printStackTrace(); }  }}class Thingy {  String name,stuff;  public Thingy(String name, String stuff) { this.name=name; this.stuff=stuff; }  public String getName() { return name; }  public String getStuff() { return stuff; }}
 

Posts:18,384
Registered: 21.03.00
Re: Comparing characters in Java  
Aug 7, 2004 10:58 AM (reply 5 of 11)



 
bbritta, that wasn't nice. Now you have completed his assignment . Won't he learn more by trying to solve them problem?

/Kaj
 

Posts:8,813
Registered: 10/4/00
Re: Comparing characters in Java  
Aug 7, 2004 11:05 AM (reply 6 of 11)



 
I didn't complete his assignment. I merely showed how to loop through a file and perform different actions depending on the input. He had the logic backwards and had actually put some thought into this. It was just a little off. While I think doing someone elses homework for them is bad, I also think that making everyone come up with every concept on their own is equally bad.

@OP, Now it's up to you to do something with it. Come back and show us what you did.
 

Posts:12
Registered: 8/7/04
Re: Comparing characters in Java  
Aug 7, 2004 11:59 AM (reply 7 of 11)



 
Thank you very much for your help. Unfortunately, I'm still a student, so I don't properly understand bbritta's coding, but I appreciate the effort. (So don't worry, my assignment is far from complete). This is my coding, that seemed to work so far, but then I get an error. seqfile is the opened file; filenum is the number of lines that the file contains; SeqArr[] is the array storing the sequences.

Code:
seqline = seqfile.readLine();
for (int j=0; j<filenum; j++) {
seqline = seqfile.readLine();
String sequence = " ";
while ((seqline.charAt != '>') && (seqline != null)){
sequence = seqline + sequence;
SeqArr[j] = sequence;
seqline = seqfile.readLine();
}
}

I then get an error for my line, while ((seqline.charAt != '>') && (seqline != null)){.
It complains of an exception in thread "main" java.lang.NullPointerException. I can't see how it's accessing something null. Please help!
 

Posts:8,813
Registered: 10/4/00
Re: Comparing characters in Java  
Aug 7, 2004 12:05 PM (reply 8 of 11)



 
My code essentially has an outer loop just to read each line from the file until it reaches the end of the file. Then, Inside that loop, I make a decision based on the first character of the line.

Regardless if the loop you use to read the file, it seems you just want to take two different actions depending on whether the first character is '>' or not. That screams 'if' statement to me.
 

Posts:18,384
Registered: 21.03.00
Re: Comparing characters in Java  
Aug 7, 2004 12:15 PM (reply 9 of 11)



 
Hi,

The following line will cause a nullpointer exception:

  while ((seqline.charAt != '>') && (seqline != null)){


If seqline is null. Change the order to;

  while (seqline != null && seqline.charAt != '>') 


/Kaj
 

Posts:8,813
Registered: 10/4/00
Re: Comparing characters in Java  
Aug 7, 2004 12:20 PM (reply 10 of 11)



 
I was wrong. You have an outer loop to read the file (a for() loop, but I assume you know what you are doing). Your inner loop reads strings and appends them while the first character is not a '>'. This is valid.
 

Posts:12
Registered: 8/7/04
Re: Comparing characters in Java  
Aug 7, 2004 2:13 PM (reply 11 of 11)



 
Thanks kajbj! It works beautifully now!
And thanks bbritta, it helps to know other methods!
 
This topic has 11 replies on 1 page.