简体   繁体   中英

Java - Reading A Double From A Text File

I am trying to read through a file and determine how many numbers (separated by spaces) there are in the line. If there is one number then that number is set as the radius of a circle and a circle object of that radius is created. Similar actions are taken with two values (a rectangle) and three values (a triangle).

I believe that the error I am getting is occurring because of a problem with my code that takes numbers from the text file, which are strings, and converts them to doubles using valueOf on line 27 of my driver class.

The issue that I am having is when I run my driver program I get the following error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "in7.txt"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at java.lang.Double.valueOf(Double.java:502)
    at Assignment7.main(Assignment7.java:27)

Here is my driver class:

import java.util.*;
import java.io.*;
public class Assignment7
{
   public static void main(String[] theArgs)
   {
      String filename = "in7.txt";
      int shapeNum;
      List<Double> shapeValues = new ArrayList<Double>();
      Shape myShape;
      double d;
      Scanner s = new Scanner(filename);
      try 
      {
         if (!s.hasNextLine())
         {
            throw new FileNotFoundException("No file was found!");
         }
         else
         {
            while (s.hasNextLine())
            {
               shapeNum = 0;
               Scanner s2 = new Scanner(s.nextLine());
               while (s2.hasNext())
               {
                  d = Double.valueOf(s2.next());
                  shapeNum++;
                  shapeValues.add(d);
               }
               if (shapeNum == 1)
               {
                  myShape = new Circle(shapeValues.get(0));
               }
               else if (shapeNum == 2)
               {
                  myShape = new Rectangle(shapeValues.get(0), 
                  shapeValues.get(1));
               }
               else
               {
                  myShape = new Triangle(shapeValues.get(0),
                  shapeValues.get(1), shapeValues.get(2));
               }
               shapeValues.clear();
               System.out.println(myShape);
            }
         }
         s.close();
      } 
      catch (FileNotFoundException e) 
      {
         System.out.println("File not found!" + e);
      }
   }
}

I've been fiddling with this code for an hour and I cannot get it to run correctly. Some help would be greatly appreciated. Thanks!

you should pass a file to the scanner . like this

File filename = new File("in7.txt");
Scanner s = new Scanner(filename);

currently you are passing a String in7.txt and that's why you get error

NumberFormatException: For input string: "in7.txt"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM