输入文件(内容):
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我的守则是:
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);
}
}当我试图运行该文件时,会得到以下错误:
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)所有文件都已正确导入,只有此错误才会出现。输入文件路径也是正确的。请帮帮忙。
发布于 2015-11-14 18:45:14
您正在尝试将一个字符串解析为一个数值,但是这个字符串不能被解析--当然,如果解析它,“8 8dict”的值是什么?
您应该尝试只解析每个字符串的第一个数值。
使用RegExs的解决方案
替换
dataset.setValue(mobilebrands[i], Double.parseDouble( s.nextToken( ) ));使用
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)));发布于 2015-11-14 18:56:39
替换这个
dataset.setValue(mobilebrands[i], Double.parseDouble( s.nextToken( ) ));使用
dataset.setValue(s.nextToken( ), Double.parseDouble( s.nextToken( )));因为当进入循环时,StringTokenizer中的初始值将是8 8dict而不是3,这是一个字符串而不是数字。
发布于 2018-08-19 05:53:23
您应该更改输入文件的内容如下:
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变量out的值是:
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使用nextToken()方法,您可以从变量的中获取令牌。第一个令牌包含字母和数字,第二个令牌只包含数字,依此类推。
Object token = s.nextToken();
您将获得包含字母和数字(8 8dict)的第一个令牌。
token = s.nextToken();您将得到第二个令牌,它只包含数字(3)。
Double.parseDouble(token.toString())
从1点到3点,你必须放进while循环。
代之以:
while(s.hasMoreTokens() && (mobilebrands[i] != null))
{
dataset.setValue(mobilebrands[i], Double.parseDouble(s.nextToken()));
i++;
}在这方面:
while(s.hasMoreTokens() && (mobilebrands[i] != null))
{
Object token = s.nextToken();
token = s.nextToken();
dataset.setValue(mobilebrands[i], Double.parseDouble(token.toString()));
i++;
}https://stackoverflow.com/questions/33711867
复制相似问题