Results 1 to 7 of 7
 03022011, 04:48 AM #1Member
 Join Date
 Mar 2011
 Posts
 3
 Rep Power
 0
Read in From file to array and convert to double
I am just doing challenges with friends and I am trying to input points from a file that represent coordinates for a triangle. The program works if I hard code in the values for the points. I am reading in the file using DataInputStream and saving it to a string variable. Then converting it to double using parseDouble and assigning it to an array. Then I assign the elements of the array to individual points. But for some reason it says the points are 0. I know the readLine is depricated but using readUTF doesn't work either. Are there any suggestions? I have included the entire code below.
import static java.lang.Math.sqrt;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MyTraingleReadIn {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Autogenerated method stub
double x1;
double x2;
double x3;
double y1;
double y2;
double y3;
double[] array;
array = new double[6];
int counter = 0;
File file = new File("MyFile.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {
// this statement reads the line from the file and print it to
// the console.
String strIn = dis.readLine();
array[counter] = Double.parseDouble(strIn);
counter ++;
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
// e.printStackTrace();
} catch (IOException e) {
// e.printStackTrace();
}
// initialize points
x1 = array[0];
//System.out.print(x1);
x2 = array[1];
x3 = array[2];
y1 = array[3];
//System.out.print(y1);
y2 = array[4];
y3 = array[5];
//declare sides and points
double side1;
double side2;
double side3;
double result;
side1 = sides(x1,x2,y1,y2);
side2 = sides(x1,x3,y1,y3);
side3 = sides(x2,x3,y2,y3);
if(isValid(side1,side2,side3)){
result = area(side1,side2,side3);
System.out.println(result);
}else{
System.out.println("Input is invalid!");
}
}
//comment: returns true if the sum of any two sides is greater than the third side
public static boolean isValid(double side1, double side2, double side3){
boolean bln = false;
double sum1 = side1 + side2;
double sum2 = side1 + side3;
double sum3 = side2 + side3;
if(sum1 > side3){
bln = true;
}else if (sum2 > side2){
bln = true;
}else if(sum3 > side1){
bln = true;
}else{
bln = false;
}
return bln;
}
//comment: returns area of the triangle
public static double area(double side1, double side2, double side3){
double s;
double area;
s = (side1 + side2 + side3)/2;
area = sqrt(s*(s  side1)*(s  side2)*(s  side3));
return area;
}
//determine sides
public static double sides(double x1,double x2,double y1,double y2){
double side;
side = distance(x1,x2,y1,y2);
return side;
}
//Distance formula
public static double distance(double x1, double x2, double y1, double y2){
double distance;
distance = sqrt((x2x1)*(x2x1)+(y2y1)*(y2y1));
return distance;
}
}Last edited by jlwhite; 03022011 at 04:53 AM.
 03022011, 05:10 AM #2
InputStreams are intended to read raw bytes and not textual data. Try using the Scanner class instead.
 03022011, 05:16 AM #3
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
And once you read the inputs as strings, use wrappers to convert into double. But the representation (or the format) should be in digits or a pattern like doubles. Otherwise you endup with format exception.
 03022011, 05:49 AM #4Member
 Join Date
 Mar 2011
 Posts
 3
 Rep Power
 0
ok well I am new to java programming and I don't know all the syntax and functions and stuff but this is what i'm trying to do to convert and it gives me a format exception
while (scanner.hasNextLine()) {
String strIn = scanner.nextLine();
array[counter] = Double.valueOf(strIn.trim());
 03022011, 05:51 AM #5Member
 Join Date
 Mar 2011
 Posts
 3
 Rep Power
 0
Nevermind it worked. The Text file was UTF8 encoded and I had to change it to ANSI. Thanks for the help!
 03022011, 10:20 AM #6
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
As I explain in my previous post, format can cause lots of issues. So handling them is very important. With trycatch you can do it easily, as you did.
However, don't these kind of thin in try block,
Java Code:// dispose all the resources after using them. fis.close(); bis.close(); dis.close();
 03022011, 10:20 AM #7
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
Similar Threads

Help needed: read array from file and then use it
By ChicagoAve in forum New To JavaReplies: 11Last Post: 02182011, 02:02 PM 
read only double input from text file
By napi1234 in forum New To JavaReplies: 6Last Post: 06282010, 04:06 PM 
Read File into 2d array
By almjodla in forum New To JavaReplies: 8Last Post: 03232010, 03:55 PM 
Anyone know how to take info from a txt file and convert it to a char array?
By 2potatocakes in forum New To JavaReplies: 9Last Post: 09112008, 02:51 AM 
initialize a number, which is read in from a file, into an array
By little_polarbear in forum New To JavaReplies: 19Last Post: 06102008, 03:53 AM
Bookmarks