Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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:
foreach (<DATA>) {
       my @line = split(/, /);
       push @table,\@line;
       }
for ($col=0; $col<@{$table[0]}; $col++) {
       $mv = 0;
       for ($row=0; $row<@table; $row++) {
               $mv = $table[$row][$col] if ($table[$row][$col] > $mv);
       }
       for ($row=0; $row<@table; $row++) {
               $table[$row][$col] /= $mv;
       }
}
foreach (@table) {
       print (join(", ",@{$_}),"\n");
       }
__END__
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


and the results:

Code:
0.75, 0.808743169398907, 0.972972972972973, 1, 0, 0.779582366589327, 0.274038461538462, 1, 1
0.125, 0.46448087431694, 0.891891891891892, 0.828571428571429, 0, 0.617169373549884, 0.153409090909091, 0.62, 0.5
1, 1, 0.864864864864865, 0, 0, 0.540603248259861, 0.293706293706294, 0.64, 1
0.125, 0.486338797814208, 0.891891891891892, 0.657142857142857, 0.55952380952381, 0.651972157772622, 0.0729895104895105, 0.42, 0.5
0, 0.748633879781421, 0.540540540540541, 1, 1, 1, 1, 0.66, 1
0.625, 0.633879781420765, 1, 0, 0, 0.593967517401392, 0.0878496503496504, 0.6, 0.5


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:
public class Normalisefile {
public static void main(String [] args) {
Datamatrix info = new Datamatrix(args[0]);
info.normalise();
info.save(args[1]);
}
}


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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho