NumberformatException JFreeChart
NumberformatException JFreeChart
输入文件(内容):
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)
所有文件都已正确导入,只是出现此错误。输入文件路径也是正确的。
请帮忙。
你想把一个字符串解析成一个数值,但是这个字符串当然解析不了,如果你解析的话,“8dict”的值是多少?
您应该尝试只解析每个字符串的第一个数值。
使用 RegEx 的解决方案
替换
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)));
替换这个
dataset.setValue(mobilebrands[i], Double.parseDouble( s.nextToken( ) ));
有
dataset.setValue(s.nextToken( ), Double.parseDouble( s.nextToken( )));
因为在进入循环时,StringTokenizer 中的初始值将是 8dict 而不是 3,这是一个字符串而不是数字。
您应该像这样更改输入文件的内容:
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() 方法,您可以从变量 s 获取标记。第一个标记包含字母和数字,第二个标记仅包含数字,依此类推。
- 您必须将 nextToken() 方法分配给变量 token.
Object token = s.nextToken();
您获得第一个包含字母和数字的标记 (8dict)。
您必须再次将 nextToken() 方法分配给变量 token。
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++;
}
输入文件(内容):
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)
所有文件都已正确导入,只是出现此错误。输入文件路径也是正确的。 请帮忙。
你想把一个字符串解析成一个数值,但是这个字符串当然解析不了,如果你解析的话,“8dict”的值是多少?
您应该尝试只解析每个字符串的第一个数值。
使用 RegEx 的解决方案
替换
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)));
替换这个
dataset.setValue(mobilebrands[i], Double.parseDouble( s.nextToken( ) ));
有
dataset.setValue(s.nextToken( ), Double.parseDouble( s.nextToken( )));
因为在进入循环时,StringTokenizer 中的初始值将是 8dict 而不是 3,这是一个字符串而不是数字。
您应该像这样更改输入文件的内容:
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() 方法,您可以从变量 s 获取标记。第一个标记包含字母和数字,第二个标记仅包含数字,依此类推。
- 您必须将 nextToken() 方法分配给变量 token.
Object token = s.nextToken();
您获得第一个包含字母和数字的标记 (8dict)。
您必须再次将 nextToken() 方法分配给变量 token。
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++;
}