import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Readtxt_1
{
public static void readfile()
{
try{
FileReader reader = new FileReader("test.txt");
BufferedReader br = new BufferedReader(reader);
String s1 = null;
int line=0;
while((s1 = br.readLine()) != null) {
++line;
//System.out.println("line:"+line);
String[] ss = s1.split(" ");
19行: System.out.println(ss[s1]);
}
br.close();
reader.close();
}catch(IOException e){
}
}
public static void main (String args[])
{
readfile();
}
}提示错误!!!
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from String to int at Readtxt_1.readfile(Readtxt_1.java:19)
at Readtxt_1.main(Readtxt_1.java:28)
import java.io.FileReader;
import java.io.IOException;
public class Readtxt_1
{
public static void readfile()
{
try{
FileReader reader = new FileReader("test.txt");
BufferedReader br = new BufferedReader(reader);
String s1 = null;
int line=0;
while((s1 = br.readLine()) != null) {
++line;
//System.out.println("line:"+line);
String[] ss = s1.split(" ");
19行: System.out.println(ss[s1]);
}
br.close();
reader.close();
}catch(IOException e){
}
}
public static void main (String args[])
{
readfile();
}
}提示错误!!!
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from String to int at Readtxt_1.readfile(Readtxt_1.java:19)
at Readtxt_1.main(Readtxt_1.java:28)
解决方案 »
- 论面向对象之死,注定了失败的道路
- JPanel不能显示在JFrame中..请问是怎么加事
- Hibernate如何开二级缓存?
- 问大家一个关于浮点数在计算中的问题
- SQL的一点小问题,各位帮忙给答一下
- 一个有关Calendar的小问题!
- 如何做一个主窗体和子窗体?
- 如何存取sqlserver2000中类型为text的字段和Access数据库中类型为文本的字段
- 小弟有大量可用国人email(广告专用,非法目的物扰)带有用户真实性名,年龄,所在地,专业,薪资水平,欲出售价格面议如有需要可以联系D
- 有关于java开发图表控件方面的书吗?请介绍一下
- 关于java输出的问题
- 通过命令行调用添加硬件或者驱动安装向导
a1 001 北京
a2 002 天津
a3 003 上海
a4 004 湖北
a5 005 武汉
a6 006 安徽
a7 007 内蒙古我想把这些txt文本写到mysql里
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;/*
D:\\test.txt
a1 001 北京
a2 002 天津
a3 003 上海
a4 004 湖北
a5 005 武汉
a6 006 安徽
a7 007 内蒙古
*/
public class ReadFile {
public static void readfile() {
try {
FileReader reader = new FileReader("D:\\test.txt");
BufferedReader br = new BufferedReader(reader);
String s1 = null;
int line = 0;
while ((s1 = br.readLine()) != null) {
++line;
System.out.println("line:"+line);
String[] ss = s1.split(" ");
for(int i = 0; i < ss.length; i++ ) {
System.out.println(ss[i]);
}
}
br.close();
reader.close();
} catch (IOException e) {
}
} public static void main(String args[]) {
readfile();
}
}
但是你却给了s1这个字符串.所以就抛异常了.你参考4楼的代码吧 .
Exception in thread "main" java.lang.NumberFormatException: For input string: "a1 001 北京"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)
at Readtxt_1.readfile(Readtxt_1.java:19)
at Readtxt_1.main(Readtxt_1.java:28)