• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading the first word of each line from a text file and then adding them to a Combo box  RSS feed

 
Owen Lai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I've been asked to read the first word of a text file of every line which looks like the following...

Sau3AI GATC 0
RsaI GTAC 2
PstI CTGCAG 5
HhaI GCGC 3
BamHI GGATCC 1
EcoRI GAATTC 1

and then make them as an item in a JComboBox. Java isn't really my strong point as I'm a technically a biologist and find the computing a bit tricky, so I would be really grateful for any help

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you have so far ? Did you read the file ?
 
Josh Priest
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am crazy new too but here is a basic framework for reading a file:





You can remove the output file stuff but it gives you the basic idea.

to store the values as a variable, you use the following format.

firstString = inFile.next();
secondString = inFile.next();
firstInt = inFile.nextInt();

Give it a spin, let us know where you get hung up.
 
Owen Lai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Managed to read it the words using the Scanner method and the reading each word using the split method and create strings using tokens,

so ive got...

Sau3AI RsaI PstI HhaI BamHI EcoRI as one string
GATC GTAC CTGCAG GCGC GGATCC GAATTC as another
and then the numbers as another one

How do I go about sorting the first string (Sau3AI...) alphabetically first and then sticking it into the combo box???
 
Rok Štelcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You could add items into the list and then call Collections.sort(list) (or Arrays.sort() in case you prefer to work with arrays).
Afterwards you would (re)add the sorted items into the combo box.

Another more general solution would be to create your own sort model ... meaning that item would be sorted immediately after insertion.


Regards,
Rok
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the examples of Readers and Writers.
Would you use a Reader and a Writer in the same loop?
Have you come across how you close a Reader or Writer in a finally block? I have found an old thread where I showed how to do that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!