Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you have so far ? Did you read the file ?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 74783
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic