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.

Quote:

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 Auto-generated 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((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));

return distance;

}

}