1

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!

1
  • new Scanner() doesn't take a filename. You're just processing the filename as a string, not the contents of the file. Commented Aug 12, 2015 at 3:25

1 Answer 1

3

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"
Sign up to request clarification or add additional context in comments.

6 Comments

I see now, the only problem with that is now I'm getting a new error. Would you happen to know why I am getting Assignment7.java:12: error: unreported exception FileNotFoundException; must be caught or declared to be thrown?
@Trafton you should handle the filenotfound exception .put try catch block
it means Scanner s = new Scanner(filename); throws a FileNotFoundException. You have to catch it or throw it
Oh, but I thought I already took care of that with the try/catch I have for the FileNotFoundException. Are you saying I need another one inside that one?
Doh! I didn't notice that. Thank you for all the help, I'm getting an output now!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.