Results 1 to 7 of 7
  1. #1
    jlwhite is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default 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 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;
    }

    }
    Last edited by jlwhite; 03-02-2011 at 03:53 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    InputStreams are intended to read raw bytes and not textual data. Try using the Scanner class instead.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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 end-up with format exception.

  4. #4
    jlwhite is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    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());

  5. #5
    jlwhite is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Nevermind it worked. The Text file was UTF-8 encoded and I had to change it to ANSI. Thanks for the help!

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by jlwhite View Post
    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
    As I explain in my previous post, format can cause lots of issues. So handling them is very important. With try-catch 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();
    due to some errors it may not release. :) Do them in finally block to be in safe side.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by jlwhite View Post
    Nevermind it worked. The Text file was UTF-8 encoded and I had to change it to ANSI. Thanks for the help!
    Good to see.

    Why don't you mark the thread solved then?

Similar Threads

  1. Help needed: read array from file and then use it
    By ChicagoAve in forum New To Java
    Replies: 11
    Last Post: 02-18-2011, 01:02 PM
  2. read only double input from text file
    By napi1234 in forum New To Java
    Replies: 6
    Last Post: 06-28-2010, 04:06 PM
  3. Read File into 2d array
    By almjodla in forum New To Java
    Replies: 8
    Last Post: 03-23-2010, 02:55 PM
  4. Replies: 9
    Last Post: 09-11-2008, 02:51 AM
  5. initialize a number, which is read in from a file, into an array
    By little_polarbear in forum New To Java
    Replies: 19
    Last Post: 06-10-2008, 03:53 AM

Posting Permissions

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