How can I create a List of a custom class with two fields and get output?

I need to make list of a gene sequence in Java and the starting positions of the parts that I care. I use a .txt file as input then search "GT" in the sequence. Once I find it, I want to store it in an array with the corresponding index. I couldn't make it happen. Please guide me.

import; import; import; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; class Signal{ public String signal; public int pos; public Signal(String s, int p) { s = signal; p = pos; } public static String inputSequence(String filename) throws IOException{ FileInputStream stream = new FileInputStream(new File(filename)); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb =, 0, fc.size()); return Charset.defaultCharset().decode(bb).toString(); } finally{ stream.close(); } } public static void main(String[] args) { String c1 = "GT"; String seq = null; List<Signal> s2 = new ArrayList<Signal>(); String s1; int i; try { seq = inputSequence(path); } catch (IOException ex) { Logger.getLogger(Signal.class.getName()).log(Level.SEVERE, null, ex); } for (i = 0; i < seq.length() - 2; i++ ) { s1 = seq.substring(i,i+2); if (s1.equals(c1)){ Signal ss = new Signal(s1, i); s2.add(ss); } } System.out.print(s2); } }

-------------Problems Reply------------

Your constructor should be as follows:

public Signal(String s, int p) {
signal = s;
pos = p;

After that it should work fine.

You can use indexOf in a loop to find all occurrences of "GT". This is shown below:

String c1 = "GT";
String seq = null;
List<Signal> s2 = new ArrayList<Signal>();

try {
seq = inputSequence(path);
} catch (IOException ex) {
Logger.getLogger(Signal.class.getName()).log(Level.SEVERE, null, ex);

int index = seq.indexOf(c1, 0);
if(index != -1){
Signal ss = new Signal(c1, index);
index = seq.indexOf(c1, index+2);
}while(index != -1);


To print out the signals list you need to override toString in your Signal class:

public String toString(){
return "(" + signal + "," + pos + ")";

Category:java Views:0 Time:2011-02-23

Related post

  • How does one create an index on the date part of DATETIME field in MySql 2008-09-18

    How do I create an index on the date part of DATETIME field? mysql> SHOW COLUMNS FROM transactionlist; +-------------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------

  • DSL Tools: Create a new Diagram in custom code 2008-09-30

    I am using DSL Tools for Visual Studio 2005. I have a DSL where at a certain point i would like to create a new Diagram using custom code. So far, i was able to create a new Diagram by overwriting the current, already opened diagram. Code follows: FE

  • How to create SharePoint Site definition with custom lists using WSPBuilder? 2010-01-27

    I'm using WSPBuilder and I want to create a site definition this site definition contains some custom list definition (some list creates based on "Custom List" template). The site contains also some files the will copied to _layouts folder (which I k

  • Is creating new Wordpress post with custom post type and custom fields possible via XML-RPC? 2012-04-07

    I've been trying to create new blog posts on my site using python and wordpress' XML-RPC API. So I came across a wrapper and tried it, but it seems it doesn't support creating posts with custom post type and fields. So I started looking for custom co

  • Creating a Workbook with a custom name without saving it to disk 2012-04-15

    Is it possible to create a Workbook with a custom name without saving it to disk? I want to avoid the default "Workbook x" names but I don't want to require the user to save the workbook. If I save it automatically in some temporary, the user won't g

  • How do you create a Marker with a custom icon for google maps API v3? 2012-04-29

    I've been reading for a while now and I can't seem to get a custom icon for my map working. Here is my javascript: var simplerweb = new google.maps.LatLng(55.977046,-3.197118); var

  • Create SQL query in VBA using table names and fields from an Access table 2012-08-09

    I have a table, call it tblSelFields, containing the following fields: TableName, SF1, SF2, SF3... SF20. Each record in tblSelFields contains table names and the field names of user-selected fields from those tables. For instance, the records may loo

  • Can't create new account under my customer's domain 2012-09-24

    Question Summary Other Admin Center issues Provide your Domain name : Additional Details Hi, I'm helping my customer to migrate email from Domino to Windows Live. After successful created some account under my customer's domain name,

  • Creating .txt files with specific character lengths per record field. 2014-07-31

    Hi, I need to produce .txt files from my database tables. These files must have their data in specific character lengths so that each record has the exact same number of characters. Using the OutputTo function in a macro I can make the report produce

  • Create Custom Percent Data Type field In Microsoft Project 2010 2014-08-24

    Similar to % Complete or % Work Complete, I want to create custom percentage data type field which can also roll up to summary level. I do not see this data type in Project 2010, is there a workaround to achieve the same, please help. --------------S

  • How can I Create a query for the Customer table to display the average balance? 2014-09-14

    I need to create a query for the Customer table (using Where, but not Group By) to display the average balance amount for sales rep 51. This is what I did. I went to Design Query in table Customers, chose Balance and Sales Rep # for the query, then o

  • I'm having trouble creating the weighted average of customer transactions 2014-10-01

    I'm having trouble creating the weighted average of customer transactions. I'm using the standard formula for weighted average, but every time a new customer is added it disproportionately affects the total. The formula I'm using is =((AY46*AY64)+(AY

  • Why I can't create email account under my customer's domain name 2015-01-15

    Dear everyone, I'm keeping asking this question for more than 5 months and still got no answer. If it's really hard to resolve this problem, I'll have to move all my managed domain to google. If anyone here can help, it would be greatly appreciated!!

  • Creating ADO.NET mapping for custom type 2008-12-29

    I have a custom type that I created, which should be persisted in a database (in this case, SQL server) as a guid (though this question is just as valid for an object that could be stored as a string, integer, etc). When I pass this type as a paramet

  • How do you create a method for a custom object in JavaScript? 2009-02-02

    Is it like... var obj = new Object(); obj.function1 = function(){ //code } or something like that? --------------Solutions------------- var newObj = { met1 : function () { alert('hello'); } } Then, the method can be called like such : newObj.met1();

  • In C# How do you create an event with a custom event handler as part of an interface? 2009-02-25

    Whats the correct format for declaring an event handler in an interface if you are using a custom subclass of EventHandler rather than the base EventHandler class. I create the EventHandler subclass MyEventHandler for example in the delegate declarat

  • Is creating a memory dump at customer environment good? 2009-05-23

    I am facing a severe problem with my program, which gets reproduced only in the customer place. Putting logs, are not helping as I doubt the failure is happening in a third party dll. For some reasons, I couldn't get help from the library provider. I

  • Creating hypermedia links in a custom media-type 2009-08-13

    I am currently creating a set of custom media types for a RESTful api (e.g. application/ and I'm trying to identify the pros and cons of two different ways of exposing hypermedia links. If I first consider what other media types

  • Save a PDF created with FPDF php library in a MySQL blob field 2009-08-20

    I need to create a pdf file with the fpdf library and save it in a blob field in my MySQL database. The problem is, when I try to retrieve the file from the blob field and send it to the browser for the download, the donwloaded file is corrupted and

Copyright (C), All Rights Reserved.

processed in 0.155 (s). 11 q(s)