Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.io.*; public class Point { private double x; private double y; publi

ID: 3767696 • Letter: I

Question

import java.io.*;

public class Point

{

private double x;

private double y;

public Point(double x_coord, double y_coord)

{

x = x_coord;

y = y_coord;

}

}

public class PointArray

{

private Point points[];

public PointArray ( FileInputStream fileIn) throws IOException

{ try

{

BufferedReader inputStream = new BufferedReader( new InputStreamReader(fileIn));

int numberOfPoints = Integer.parseInt(inputStream.readLine());

points = new Point[numberOfPoints];

int i=0; String line;

while ((line = inputStream.readLine()) != null)

{

System.out.print(line);

double x = Double.parseDouble(line.split(" ")[0]);

double y = Double.parseDouble(line.split(" ")[1]);

points[i] = new Point (x, y); i++;

}

inputStream.close();

}

catch (IOException e)

{

System.out.println("Error");

System.exit(0);

}

}

}

public String toString()

{

String format = "{";

for (int i = 0; i < points.length; i++)

{

if (i < points.length-1)

format = format + points[i] + ", ";

else

format = format + points[i];

}

format = format + "}";

return format;

}

public static void main(String[]args)

{

FileInputStream five = new FileInputStream(new File("fivePoints.txt"));

PointArray fivePoints = new PointArray (five);

System.out.println(fivePoints.toString());

}

The txt file fivePoints is as shown below:

The first nunmber in the first line means the number of points in the text file. The output I want to get is {(2.0, 7.0), (3.0, 5.0), (11.0,17.0), (23.0, 19.0), (150.0, 1.0)}. How can I fix it?

Error:

It underline the code "double x = Double.parseDouble(line.split(" ")[0]);"

java.lang.NumberFormatExecption:

empty String (in sun.misc.FloatingDecimal)

Explanation / Answer

NOTE:
The string you're trying to parse as double is empty.
You need to check if the line.split(" ")[0]
method returns a non empty string before trying to do the parsing cause you can't parse to
double an empty string.
please check the code and out below working fine

Point.java
public class Point {
   private double x;
   private double y;

   public Point(double x_coord, double y_coord) {
       x = x_coord;
       y = y_coord;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   public String toString() {

       return "(" + x + "," + y + ")";
   }
}

PointArray.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class PointArray {

   private Point points[];

   public PointArray(FileInputStream fileIn) throws IOException {
       try {
           BufferedReader inputStream = new BufferedReader(
                   new InputStreamReader(fileIn));
           int numberOfPoints = Integer.parseInt(inputStream.readLine());
           points = new Point[numberOfPoints];
           int i = 0;
           String line;
           while ((line = inputStream.readLine()) != null) {

               double x = Double.parseDouble(line.split(" ")[0]);
               double y = Double.parseDouble(line.split(" ")[1]);
               points[i] = new Point(x, y);
               i++;
           }
           inputStream.close();
       } catch (IOException e) {
           System.out.println("Error");
           System.exit(0);
       }
   }

   public String toString() {
       String format = "{";
       for (int i = 0; i < points.length; i++) {
           if (i < points.length - 1)
               format = format + points[i] + ", ";
           else
               format = format + points[i];
       }
       format = format + "}";
       return format;
   }

   public static void main(String[] args) {
       try {
           FileInputStream five = new FileInputStream(new File(
                   "fivePoints.txt"));
           PointArray fivePoints = new PointArray(five);
           System.out.println(fivePoints.toString());
       } catch (Exception e) {
       }
   }

}
fivePoints.txt
5
2 7
3 5
11 17
23 19
150 1

OUTPUT:
{(2.0,7.0), (3.0,5.0), (11.0,17.0), (23.0,19.0), (150.0,1.0)}