| 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: 32833
|
|
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: 32833
|
|
|
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.
|
 |
 |
|
|
subject: Best option for 2 rows data
|
|
|