Question
Input is from a file called num1.in Input consists of 4 doubles representing the radii of 4 circles. You are to make an array of
Input is from a file called num1.in Input consists of 4 doubles representing the radii of 4 circles. You are to make an array of circles and then print out the radius followed by the circumference, one per line. So the output looks like this:
0.7 4.39822971502571 2.15 13.50884841043611 0.8 5.026548245743669 3.5 21.991148575128552
import java.io.*; public class Lab6Num1 {
public static class Circle { //attribute private double radius; //constructors public Circle() { radius=0.0; } public Circle(double r) { radius=r; } //accessors public double getRadius() { return radius; } //mutators public void setRadius(double r) { radius = r; } //methods public double circumference() { return 2*Math.PI*radius; } public double area() { return Math.PI*radius* radius; } } public static void main(String[] args) throws Exception {
//declare input file File file = new File("num1.in"); BufferedReader br = new BufferedReader(new FileReader(file)); //create (declare) an array that can hold Circle objects Circle[] circleObjs = new Circle[4]; //input 4 Circle objects into the array int size = 0; String st; /* * Reading through the file line by line. Each line contains one double value. * After reading each value, a circle is created and inserted into the array of circles. * This loop repeats until the end of file is found or 4 values are read, whichever is smaller */ while ((st = br.readLine()) != null && size < 4) { Double val = Double.parseDouble(st); // Taking the value from the file and parsing it to Double circleObjs[size] = new Circle(val); // Creating a Circle and pushing into the array size++; // Incrementing the number of values pushed into the array }
//output the radius and circumference of each member of the array for(int n=0;n<4;n++) { if(circleObjs[n] != null) { // Condition to check if the number of values in the file is less than 4 System.out.println(circleObjs[n].getRadius() + " " + circleObjs[n].circumference()); } } } }
when entering this program the program outputs this
Exception in thread "main" java.lang.NumberFormatException: For input string: "0.7 2.15 0.80 3.5" at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054) at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.base/java.lang.Double.parseDouble(Double.java:543) at Lab6Num1.main(Lab6Num1.java:59)
how can i fix this?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started