查看: 2043| 回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

[入门|算法|数据结构] 求救啊~~~stanford 算法课week 5.java为什么数据格式老是读不对??

全局:
公开课
学校名称: stanford
Unit号: 5
开课时间: 2013-07-16
课程全名: stanford algorithm
平台: Coursera

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
读数总是出错,我是java菜鸟,求大牛解答。



Exception in thread "main" java.lang.NumberFormatException: For input string: "1        80,982        163,8164        170,2620        145,648        200,8021        173,2069        92,647        26,4122        140,546        11,1913        160,6461        27,7905        40,9047        150,2183        61,9146        159,7420        198,1724        114,508        104,6647        30,4612        99,2367        138,7896        169,8700        49,2437        125,2909        117,2597        55,6399        "



程序为:

package day01;


import java.io.*;

public class ArrayDemo {
        public static void main(String args[]) throws Exception{
                int[] hash = new int[1000000];
                int target[] = {7, 37, 59, 82, 99, 115, 133, 165, 188, 197};
                int result[] = new int[9];
            //  BufferedReader in=new BufferedReader(new FileReader(filename));
                FileInputStream fstream = new FileInputStream("D://dijkstraData.txt");
                // Get the object of DataInputStream
                DataInputStream di = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(di));

                String s;
                while((s = br.readLine()) != null){
                        int temp = Integer.parseInt(s);
                        for(int i = 0; i < 9 ;i++)
                                if(target[i] > temp && hash[target[i] - temp] == 1)
                                        result[i] = 1;
                                         hash[temp]++;
                }

                for(int i = 0; i < 9; i++)
                        System.out.print(result[i]);
        }
}


上一篇:[Udacity] Introduction to Programming (Week 7)
下一篇:[Udacity] Introduction to Programming (Week 8)
🔗
 楼主| share1122008 2013-8-12 06:52:00 | 只看该作者
全局:
后来用c++做出来了结果交了作业。可是还是不知道为啥java读入这个文件老出错。有同学能指点一下吗?谢谢
回复

使用道具 举报

🔗
lixiang.xjtu 2013-8-12 13:46:39 | 只看该作者
全局:
int temp = Integer.parseInt(s);
s是一行,你要先split,然后再转换为int。
回复

使用道具 举报

🔗
lljllj122 2013-8-12 23:34:38 | 只看该作者
全局:
同楼上
s = br.readLine(),是从输入流里面读取出来的一行,没法直接用Integer.parseInt这个方法。
\
回复

使用道具 举报

🔗
lljllj122 2013-8-12 23:36:37 | 只看该作者
全局:
parseInt
public static int parseInt(String lexicalXSDInt)
Convert the string argument into an int value.

Parameters:
lexicalXSDInt - A string containing a lexical representation of xsd:int.
Returns:
A int value represented by the string argument.
Throws:
NumberFormatException - lexicalXSDInt is not a valid string representation of an int value.
回复

使用道具 举报

🔗
 楼主| share1122008 2013-8-13 03:27:45 | 只看该作者
全局:
谢谢楼上的,我太粗心了,应该多多查API.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表