Search code examples
javastringexceptionnumber-formatting

unknown "NumberFormatting" Issue


(In advance, sorry for the lengthy and somewhat niche post but I'm terribly stuck) Complete Java programming newbie here, and I've been following along with the "Java ALL-IN-ONE for Dummies" book, and I've hit a snag I can't seem to get past. For some reason, my code, as well as the code taken from the book's download website, throws a NumberFormatException. My code is as follows..

`package videoRead;

import java.io.*;
import java.text.NumberFormat;

public class reader 
{

    public static void main(String[] args)
    {
        NumberFormat cf = NumberFormat.getCurrencyInstance();
        BufferedReader in = getReader("Movie.txt");
        Movie movie = readMovie(in);
        while (movie != null)
        {
        String msg = Integer.toString(movie.year);
        msg += ": " + movie.title;
        msg += " (" + cf.format(movie.price) + ")";
        System.out.print(msg);
        movie = readMovie(in);
        }

    }
    private static BufferedReader getReader(String name)
    {
        BufferedReader in = null;
        try
        {
            File file = new File(name);
            in = new BufferedReader(
                new FileReader("C:\\Users\\hunte\\Desktop\\Movie.txt") );
        }
        catch (FileNotFoundException e)
        {
            System.out.print(
                    "the file doesn't exist.");
            System.exit(0);
        }
        return in;
    }

    private static Movie readMovie(BufferedReader in)
    {
        String title;
        int year;
        double price;
        String line = "";
        String[] data;

        try
        {
            line = in.readLine();
        }
        catch (IOException e)
        {
            System.out.print("I/O Error");
            System.exit(0);
        }
        if (line == null)
                return null;
        else
        {
            data = line.split("\t");
            title = data[0];
            year = Integer.parseInt(data[1]);
            price = Double.parseDouble(data[2]);
            return new Movie(title, year, price);
        }
    }

    private static class Movie 
    {
        public String title;
        public int year;
        public double price;
        public Movie(String title, int year, double price)
        {
        this.title = title;
        this.year = year;
        this.price = price;
        }
    }

}

with the error code being

`1946: It's a Wonderful Life ($14.95)1972: Young Frankenstein ($16.95)1973: Star Wars ($17.95)1987: The Princess Bride ($14.95)1989: Glory ($14.95)Exception in thread "main" java.lang.NumberFormatException: For input string: "14.95"
        at java.base/java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.base/java.lang.Integer.parseInt(Unknown Source)
        at java.base/java.lang.Integer.parseInt(Unknown Source)
        at videoRead/videoRead.reader.readMovie(reader.java:65)
        at videoRead/videoRead.reader.main(reader.java:20)`

My question is why is this happening, and how can I fix it? or how do I go about catching the exception that won't break the code?

(Also, if anyone could tell me why my code won't split lines, that would be awesome as well)

Thanks!!


Solution

  • According to error message this is the format of string that you have in text file, so below code split the line based on space delimiter and filter the values by removing all extra characters

    Note: This code works if each each record is a separate line with this format in text file

    String s ="1946: It's a Wonderful Life ($14.95)";
    String[] ar = s.split(" ");
    System.out.println(ar[0].substring(0, ar[0].length()-1));
    String str = String.join("," ,Arrays.copyOfRange(ar, 1, ar.length-2)).replaceAll(",", " ");
    System.out.println(str);
    System.out.println(ar[ar.length-1].substring(2, ar[ar.length-1].length()-1));
    

    Output:

    1946
    It's a Wonderful
    14.95