Whenever I create an input text file and run the application I get this error message.    Exception in thread "main" java.lang.NumberFormatException: empty String at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842) at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.base/java.lang.Double.parseDouble(Double.java:651) at javaapplication3.RegularPolygonMain.main(RegularPolygonMain.java:30)   How do I fix this?   Code:  File 1:  package javaapplication3; public class RegularPolygon { private int numSides;  private double side; private double x; private double y; RegularPolygon() {  this.numSides = 3;  this.side = 1;  this.x = this.y = 0; } RegularPolygon(int sides, double sideLength) {  this.numSides = sides;  this.side = sideLength;  this.x = this.y = 0; }                RegularPolygon(int sides, double eachSideLen, double x1, double y1) {  this.numSides = sides;  this.side = eachSideLen;  this.x = x1;  this.y = y1; } public int getNumSides() {  return numSides; } public void setNumSides(int numSides) {  this.numSides = numSides; } public double getSide() {  return side; } public void setSide(double side) {  this.side = side; } public double getX() {  return x; } public void setX(double x) {  this.x = x; } public double getY() {  return y; } public void setY(double y) {  this.y = y; } public double perimeter() {            return side * numSides; } public double area() {  return (numSides * Math.pow(side, 2)) / (4 * Math.tan(Math.PI / numSides)); } }   File 2:  package javaapplication3; import java.io.File; import java.io.IOException; import java.util.Scanner; public class RegularPolygonMain { private static final String FILE_NAME = "input.txt"; public static void main(String[] args) {    try (Scanner reader = new Scanner(new File(FILE_NAME))){   while (reader.hasNext()) {        String line = reader.nextLine();        line = line.trim();        String[] values = line.split("");                                        RegularPolygon p;            int numSides = Integer.parseInt(values[0]);    double sideLength = Double.parseDouble(values[1]);            if (values.length == 2) {          p = new RegularPolygon(numSides, sideLength);    } else {          int x = Integer.parseInt(values[2]);     int y = Integer.parseInt(values[3]);     p = new RegularPolygon(numSides, sideLength, x, y);    }        printPolygonDetails(p);   }  } catch (IOException e) {   System.out.println(e.getMessage());  } } private static void printPolygonDetails(RegularPolygon p) {  System.out.print(    p.getNumSides() + " " + p.getSide() + " (" + p.getX() + "," + p.getY() + ") " + p.perimeter() + " ");    System.out.println(String.format("%.2f", +p.area())); } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Whenever I create an input text file and run the application I get this error message. 

 

Exception in thread "main" java.lang.NumberFormatException: empty String at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842) at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.base/java.lang.Double.parseDouble(Double.java:651) at javaapplication3.RegularPolygonMain.main(RegularPolygonMain.java:30)

 

How do I fix this?

 

Code: 

File 1: 

package javaapplication3;
public class RegularPolygon {
private int numSides; 
private double side;

private double x;
private double y;


RegularPolygon() {
 this.numSides = 3;
 this.side = 1;
 this.x = this.y = 0;
}

RegularPolygon(int sides, double sideLength) {
 this.numSides = sides;
 this.side = sideLength;
 this.x = this.y = 0;
}
       
       RegularPolygon(int sides, double eachSideLen, double x1, double y1) {
 this.numSides = sides;
 this.side = eachSideLen;
 this.x = x1;
 this.y = y1;
}
public int getNumSides() {
 return numSides;
}
public void setNumSides(int numSides) {
 this.numSides = numSides;
}


public double getSide() {
 return side;
}


public void setSide(double side) {
 this.side = side;
}


public double getX() {
 return x;
}


public void setX(double x) {
 this.x = x;
}

public double getY() {
 return y;
}


public void setY(double y) {
 this.y = y;
}


public double perimeter() {
           return side * numSides;
}

public double area() {
 return (numSides * Math.pow(side, 2)) / (4 * Math.tan(Math.PI / numSides));
}
}

 

File 2: 

package javaapplication3;

import java.io.File;

import java.io.IOException;

import java.util.Scanner;

public class RegularPolygonMain {


private static final String FILE_NAME = "input.txt";

public static void main(String[] args) {
 
 try (Scanner reader = new Scanner(new File(FILE_NAME))){
  while (reader.hasNext()) {
   
   String line = reader.nextLine();
   
   line = line.trim();
   
   String[] values = line.split("");
                               
   
   RegularPolygon p;
   
   
   int numSides = Integer.parseInt(values[0]);
   double sideLength = Double.parseDouble(values[1]);
   
   
   if (values.length == 2) {
    
    p = new RegularPolygon(numSides, sideLength);
   } else {
    
    int x = Integer.parseInt(values[2]);
    int y = Integer.parseInt(values[3]);
    p = new RegularPolygon(numSides, sideLength, x, y);
   }
   
   printPolygonDetails(p);
  }
 } catch (IOException e) {
  System.out.println(e.getMessage());
 }
}


private static void printPolygonDetails(RegularPolygon p) {
 System.out.print(
   p.getNumSides() + " " + p.getSide() + " (" + p.getX() + "," + p.getY() + ") " + p.perimeter() + " ");
 
 System.out.println(String.format("%.2f", +p.area()));

}

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY