Results 1 to 4 of 4
- 02-05-2010, 10:00 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 44
- Rep Power
- 0
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
-
What do you mean by "efficient"? Also, please edit your post above and add code tags (please see my signature below).
- 02-05-2010, 02:08 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 02-05-2010, 02:48 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
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
-
A difficult question
By tyang in forum New To JavaReplies: 2Last Post: 01-31-2010, 09:45 PM -
Difficult compilation
By pochis40 in forum Java AppletsReplies: 10Last Post: 12-21-2009, 12:35 PM -
A more efficient Game of Life
By unreal4evr in forum New To JavaReplies: 3Last Post: 03-27-2009, 03:08 AM -
Java's web world is really difficult..
By jurka in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-02-2008, 05:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks