Results 1 to 4 of 4
  1. #1
    tyang is offline Member
    Join Date
    Jan 2010
    Posts
    44
    Rep Power
    0

    Default A difficult question - efficient coding?

    Can anyone tell me whether the following code is efficient or not?
    Thank you very much!


    import java.io.*;
    import java.util.*; //needed for StringTokenizer
    public class Knn
    {
    public static void main(String args[])
    throws java.io.IOException
    {
    String fileName, inLine;
    int i, j, numInstance, numFeature;
    double dat[][] = new double[150][4];
    StringTokenizer st;

    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    System.out.print("Please enter the file name:");
    fileName = br.readLine() + ".txt";
    System.out.print("Please enter the number of instances:");
    numInstance = Integer.parseInt(br.readLine());
    System.out.print("Please enter the number of features:");
    numFeature = Integer.parseInt(br.readLine());

    br.close();

    BufferedReader fi = new BufferedReader(new FileReader(fileName));
    for (i = 0; i<numInstance; i++)
    {
    inLine = fi.readLine();
    st = new StringTokenizer(inLine);
    for (j = 0; j<numFeature; j++)
    {
    dat[i][j] = Double.parseDouble(st.nextToken());
    }
    System.out.println(dat[i][0]);
    }
    fi.close();


    }




    }//end class Knn

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What do you mean by "efficient"? Also, please edit your post above and add code tags (please see my signature below).

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Couldn't say since I have no idea what you're trying to achieve (and it's very difficult to read unformatted code). Or, as Fubarable says, any idea what you might mean by efficient.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by tyang View Post
    Can anyone tell me whether the following code is efficient or not?
    As far as I can see the program reads n lines and m doubles per line; the values n and m are supplied by the user. The entire program is bound by the IO speed, so speed tests are out of the question. The user IO is very fragile (if the user types nonsense or incorrect values for n and m the entire thing dies a horrible death). If n and m and the file contents are correct the program does its job though, too bad a fixed size two dimensional array was used.

    kind regards,

    Jos

Similar Threads

  1. A difficult question
    By tyang in forum New To Java
    Replies: 2
    Last Post: 01-31-2010, 09:45 PM
  2. Difficult compilation
    By pochis40 in forum Java Applets
    Replies: 10
    Last Post: 12-21-2009, 12:35 PM
  3. A more efficient Game of Life
    By unreal4evr in forum New To Java
    Replies: 3
    Last Post: 03-27-2009, 03:08 AM
  4. Java's web world is really difficult..
    By jurka in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-02-2008, 05:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •