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)
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)