wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Best option for 2 rows data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Best option for 2 rows data " Watch "Best option for 2 rows data " New topic
Author

Best option for 2 rows data

Joana Silva
Greenhorn

Joined: Feb 28, 2009
Posts: 5
hello its my first post

I have this input file style



where I add them to a string and I need to save them in any type of structure just like that alignment with 2 columns, so what would my best structure choice and how to do it ?
I thought of using ArrayList but no Idea how to do it.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Hey Joana,

Welcome to JavaRanch!

where I add them to a string and I need to save them in any type of structure just like that alignment with 2 columns, so what would my best structure choice and how to do it ?
I thought of using ArrayList but no Idea how to do it.

I am not clear about your question. How do you want to save them? How are you gonna manipulate/retrieve them? And tell us little about your file structure too.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
Welcome to JavaRanch again

Numbers in Java don't have a "format" but can be reformatted when they are printed (or put into a text file) with the % tags. You might do well to create a class which has int and char fields which will record the number and letter. As for putting them into a List, read this Java Tutorials section.

Do your formatting with the % tags, as described in the Formatter class, or here in the Java Tutorials. You can thus alter the format every time you print the data out.
Joana Silva
Greenhorn

Joined: Feb 28, 2009
Posts: 5
I forgot to mention that the input file has Chars and number on the seconds column so I have to use a string



this is my code so far :




This is the output [18690 I, 18745 10, 18971 I, 19005 11, 19015 6]

I want to store the data in two columns like in a bidimentional array and do math operations with the second column.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
Use the nextXXX and hasNextXXX methods of the Scanner class, or the split method of the String class.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
...Or If you want to read from file as you have mentioned in earlier posts, you can use FileReader and wrap it into BufferedReader. And use String#Split method.

Thereafter you can store them into two dimensional array.

Joana Silva wrote:I want to store the data in two columns like in a bidimentional array and do math operations with the second column.

How can you do math operation on string? What does that data represent? How do you want to manipulate them?
(I am still unclear about your requirement.)
Joana Silva
Greenhorn

Joined: Feb 28, 2009
Posts: 5
I will have to convert the ones with numbers to a int so i can calculate the average from the second column of those wihout Letters and I dont need to read from a file I just copy past the input file on console
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Ok then you can use Scanner class as suggested earlier. Store the values into plain two dimensional array.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Best option for 2 rows data
 
Similar Threads
Which is the efficient way to convert XML to HTML?.
Performance: Horizontal scalability or vertical scalabiltiy
General java job questions
What is the common structure for your ActionListeners?
Help with displaying data in JTextfield