| |||||||||||
reading in a file and changig the column values Posted by rebecca (rebecca), 10 December 2005 Hi,I'm new to java and i'm really struggling with this problem to the point of chucking my computer off the roof and burning it I have to write a program to normalise some data, by getting the largest value in each column and dividing every other value by this so that the data is in the range [0,1]. example of data: 6, 148, 72, 35, 0, 336, 0627, 50, 2 1, 85, 66, 29, 0, 266, 0351, 31, 1 8, 183, 64, 0, 0, 233, 0672, 32, 2 1, 89, 66, 23, 94, 281, 0167, 21, 1 0, 137, 40, 35, 168, 431, 2288, 33, 2 5, 116, 74, 0, 0, 256, 0201, 30, 1 so say for the first column i would divide everything by 8. I know i should put each column into an array then perform the calculations and then write it back to the file. i just have no idea how to code this. can anybody help please? Posted by admin (Graham Ellis), 10 December 2005 Why Java? You could do a grand job in Java, but it would be much quicker to write / maintain / change for future use, and you would have a lot less learning to do, in Python, Perl or something similar.In Perl: Code:
and the results: Code:
Footnote - I used Perl because I'm running a Perl course next week and I need to get back into it ... if next week had been a Python course, that would have been my choice. P.S. If I was teachning Java next week, I would still have chosen Python or Perl for this answer! Posted by rebecca (rebecca), 10 December 2005 I have to do it in java, because its the chosen language for my project and im not really any good at it either. a few people have told me the same thing you have but theres no getting round this one unfortunately. Cheers for the code i'll try to see if i can translate this into java. Posted by rebecca (rebecca), 10 December 2005 Hi,Do you know how i wouls start to go about doing this in java? Cheers Posted by admin (Graham Ellis), 10 December 2005 Yes, but if I did a great deal of the work for you, then you wouldn't really be learning so much Java. I'm always reluctant to get involved with student assignments when I'm not the tutor as I wouldn't want to step on your tutor's toes - he / she will know where to guide you by helping write code and where to guide you by giving explanations and letting you write the code. You are welcome to post up specific questions that crop up though, with code examples and "can you explain this" type questions.Posted by rebecca (rebecca), 10 December 2005 Hi,I have wrote some code that reads each line of a file into an array, but that's as far as i've got. i dont know where to go from here, as i don't know how to put each column into an array in order to perform the calculations. Here's the code so far: import java.io.*; import java.util.*; public class ReadFile { public static void main(String[] args) { ArrayList values = loadFile("testfile.txt"); for (int i=0; i<values.size(); i++) System.out.println(i+1 + ":\t" + values.get(i)); } public static ArrayList loadFile(String fileName) { String line; ArrayList file = new ArrayList(); try { BufferedReader in = new BufferedReader(new FileReader(fileName)); if (!in.ready()) throw new IOException(); //String[] words = line.split(","); while ((line = in.readLine()) != null) //for(int j=0;j<words.length;j++) file.add(line); //String[] column = file.split(","); in.close(); } catch (IOException e) { System.out.println(e); return null; } return file; } } each attribute in the file is numeric and comma deliminated. Could you offer any suggestions of how to put the columns into an array? I understand that you don't like to offer too much help as i wouldn't be learning myself, i really have tried to figure this one out but its going no where. Any help will be much appreciated Cheers Posted by admin (Graham Ellis), 10 December 2005 I would be inclined to use split up each line into an array of Strings, then use a loop of Integer.parseInt calls to convert it into an array of integers which I would then add onto the end of my ArrayList. Once you've done that, you've recoded the first 3 lines of my Perl and can go on to the easier conversion / calculation that follows Posted by admin (Graham Ellis), 11 December 2005 It struck me that I may appear a little negative about Java. Let me clarify that - it's a great language for medium to larger scale systems where the same data formats and code (oops - classes) are to be used may times over. You'll find that initial coding of an application / set of classes takes much longer than in other laguages, but in time with code re-use each extra step and utility process will be written that much quicker.With pre-written classes already available to her, Rebecca's application could become as simple as: Code:
But that does require the Datamatrix class to already exist from a linked application, with an appropriate constructor, and normalise and save methods. Posted by rebecca (rebecca), 12 December 2005 Hi,are you suggesting i should scrap the idea of using arrays or vectors and look into data matrix's? would it be easy to convert the data matrix back into a text file again? Thanks for your help Rebecca Posted by admin (Graham Ellis), 12 December 2005 No, I wasn't; perhaps I wasn't very clear in what I wrote. I was simply saying that you would / could / should place the complex parts of your structure into your own class in Java (for code re-use purposes) and I made up the name DataMatrix ... it will still contain the stuff yhou have, but it will be "hidden within" or - the posh official word - encapsulated.Posted by rebecca (rebecca), 12 December 2005 right ok, sorry didn't know what you were getting at. i'd searched the web for datamatrix and it bought up all this stuff about barcode readers I'll give it a go Cheers This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |