- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
输入文件(内容):
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”的值是多少?
您应该尝试仅解析每个字符串的第一个数值。
<小时/>替换
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)));
关于java - NumberformatException JFreeChart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33711867/
我正在尝试使用 Integer.parseInt(simNumber) 将 String 转换为 int,但我收到错误消息。如何将 String 转换为 int?我在网上搜索了一下,找到了Intege
每次我尝试启动我的代码时,我总是遇到同样的错误: java.lang.NumberFormatException: For input string: "x" at java.lang.Numbe
在检查我的错误处理代码时偶然发现了这个。当您调用 Integer.parseInt(null) 时,Java 抛出 NumberFormatException 而 Float.parseFloat(n
如果有人可以看看我在 Selenium 方面遇到的问题。我尝试使用以下方法访问上述计算器中的结果: public String getAttributeValue(WebElement element
嗯,我很无聊,所以我想做一个二进制>十进制转换器。我将转换器的上限设置为 30 位数字 (1073741823) 或 (0111111111111111111111111111111)。我遇到的问题是
在以下代码中,Clojure (1.2) 打印出错误消息: (try (let [value "1,a"] (map #(Integer/parseInt %) (.split value
String str="123456.7855456677"; ParsePosition parsePosition = new ParsePosition(0); NumberFormat num
代码: String myVar = "1255763710960"; int myTempVar=0; try { myTempVar = Integer.valueOf(myVar); }
我在NumberFormatException代码中收到Grails错误,该错误是将电影分类到数据库中。该错误表明它来自cron插件。我已经完成了研究,并且一直在尝试使用NumberFormatExc
我在将字符串解析为原语时遇到问题。 File file1 = new File("bankData.txt"); Scanner sc = new Scanner(fi
所以我试图从日期列表中查找最新日期,但我不断收到 NumberFormatException。有什么办法可以解决这个问题吗? import java.util.*; public class Date
如果使用变量 bin1 它不会转换,但是如果我用 bin2 替换参数,它似乎可以工作。 我尝试使用 long 而不是 int。还是一样。 public class Test{ public s
我正在尝试找出如何在代码中捕获数字格式异常错误,这样如果用户在字符串中输入字母并且我的程序尝试将其解析为 int 我的程序将不会抛出错误而是停止并返回一个 boolean 值。我还试图了解,如果 tr
该程序的本质目的是将学生信息写入文件并从同一文件中读取。该程序包括一个 if 语句,用于判断学生是否处于良好信誉或留校察看状态,每个语句都有各自的文件(goodstand.txt 和 probatio
我正在用 Python/WLST 编写代码,以自动连接、启动和停止 weblogic 中的托管服务器。当我启动 Python 时,出现以下错误。 异常的含义是什么?我该如何解决它? Initializ
这是我的代码: public void setAreaAccessPoints(){ String mac = "",essid = "",status = ""; int stre
我正在用 Java 开发一个应用程序,发现了这个奇怪的行为: 如果通过控制面板将区域设置格式设置为匈牙利语(系统默认值),我会收到此异常,但如果我将其设置为英语格式,则它可以正常工作。也适用于虚拟 M
我正在尝试制作一个计算器应用程序,但不幸的是我无法解析字符串。 应用程序接受输入并将其显示在 EditText et2 中。当按下“等于”键时,计算器将执行以下工作。 et2 = (EditText)
我有一个文件,其中包含许多电影标题及其年份、5 星评级和长度。我正在尝试读取该文件并将标题、年份、评级和长度存储为变量。我遇到的问题是在我的检索年份的代码中。该代码可以编译,但在运行时,当它到达具有多
我正在解决亚马逊 Interview Street 网站上的 Connected Sets 问题 https://amazon.interviewstreet.com/challenges我的代码对于
我是一名优秀的程序员,十分优秀!