Search code examples
javajfreechart

NumberformatException JFreeChart


Input file(contents):

8dict, 3
9GAG, 2
RT, 7
The, 2
awkward, 2
co, 14
http, 11
https, 3
irrfan_k, 2
is, 2
my, 3
rainymornings, 3
t, 14
the, 2
this, 2
you, 3

My Code is:

package pck;

import java.io.*; 
import java.util.StringTokenizer; 
import org.jfree.chart.ChartUtilities; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.JFreeChart; 
import org.jfree.data.general.DefaultPieDataset;

public class PieChart_File 
{
   public static void main( String[ ] args )throws Exception
   {
      String mobilebrands[ ] = {    
        "8dict" ,   
        "9GAGs" ,   
        "RT" ,    
        "The" ,
        "awkward",
        "co",
        "http",
        "https",
        "irrfan_k",
        "is",
        "my",
        "rainymornings",
        "t",
        "the",
        "this",
        "you"
      };

      InputStream in = new FileInputStream( new File( "C:/temp/test.txt" ) );          
      BufferedReader reader = new BufferedReader(new InputStreamReader(in ) );          
      StringBuilder out = new StringBuilder();          
      String line;          
      DefaultPieDataset dataset = new DefaultPieDataset();          

      while (( line = reader.readLine() ) != null ) 
      {
         out.append( line );
      }
      StringTokenizer s = new StringTokenizer( out.toString(), "," );
      int i=0;      
      while( s.hasMoreTokens( ) && ( mobilebrands [i] != null ) )
      {

          dataset.setValue(mobilebrands[i], Double.parseDouble( s.nextToken( ) ));
         i++;
      }
      JFreeChart chart = ChartFactory.createPieChart( 
         "Repeated words",  // chart title           
         dataset,         // data           
         true,            // include legend           
         true,           
         false);

      int width = 560; /* Width of the image */          
      int height = 370; /* Height of the image */                          
      File pieChart = new File( "pie_Chart.jpeg" );                        
      ChartUtilities.saveChartAsJPEG( pieChart, chart, width, height); 
   }
}

I get the following error when I try to run the file:

Exception in thread "main" java.lang.NumberFormatException: For input string: "8dict"
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at pck.PieChart_File.main(PieChart_File.java:48)

All files are properly imported only this error shows up. The input file path is also proper. Please help.


Solution

  • You're trying to parse a String into a numeric value but the String can not be parsed of course, what is the value of "8dict" if you parse it?

    You should try to parse only the first numeric value of each String.


    Solution using RegExs

    Replace

    dataset.setValue(mobilebrands[i], Double.parseDouble( s.nextToken( ) ));
    

    With

    Pattern p = Pattern.compile("\\d*"); // Finds the digits in a given String
    Matcher m = p.matcher(s.nextToken());
    m.find();
    dataset.setValue(mobilebrands[i], Double.parseDouble(m.group(0)));