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)

解决方案 »

  1.   

    我真不会转换了。 谢谢大家帮帮我。
    a1 001 北京
    a2 002 天津
    a3 003 上海
    a4 004 湖北
    a5 005 武汉
    a6 006 安徽
    a7 007 内蒙古我想把这些txt文本写到mysql里
      

  2.   

    System.out.println(ss[Integer.valueOf(s1)]); 
      

  3.   

    19行: System.out.println(ss[s1]); 这是什么???还有 数组的用法也不对。 ss[s1] 这里只能是int 型的。 
      

  4.   


    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();
    }
    }
      

  5.   

    你这个报错是由于你定义的String s1 = null; 这里是字符型.而你在取空格分隔收的数组时,本应该通过int的下标来取的.
    但是你却给了s1这个字符串.所以就抛异常了.你参考4楼的代码吧 .
      

  6.   

    这代码编译不过,数组下标不能使用String.
      

  7.   

    System.out.println(ss[Integer.valueOf(s1)]); 提示错误
    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)