如何从文件中读取数据到Java数组? [英] how to read data from file into array in Java?

查看:105
本文介绍了如何从文件中读取数据到Java数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从文本文件中一些帮助数据读入我的的ArrayList 。随着创建和把的ArrayList 到文本文件中的第一部分完美的作品。我只需要在标记区域结束一些帮助。

请注意,有些标识符是我的母语。

 公共类ContAngajat {
   字符串用户名;
   字符串密码;
}公共类CreazaCont {//创建ArrayList和将其放入一个文件公共静态无效昂(字符串ARGS []){    ArrayList的< ContAngajat> angajati =新的ArrayList< ContAngajat>(50); 扫描仪diskScanner =新的扫描仪(中); 扫描仪福尔=新的扫描仪(中);
 INT N;     的out.print(Introduceti numarul德conturi NOI关心doriti SA乐introduceti:);
     N = forn.nextInt();
  通过out.println();     尝试{    FileWriter的FW =新的FileWriter(ConturiAngajati.txt,真正的);
    的for(int i = 0; I< N;我++){
  ContAngajat续=新ContAngajat();  的out.print(用户名:);
  cont.username = diskScanner.nextLine();
  的out.print(密码:);
  cont.password = diskScanner.nextLine();  angajati.add(续)  fw.write(cont.username +);
  fw.write(cont.password +|);
  }
  fw.close();
     }
     赶上(IOException异常前){      的System.out.println(无法写入文件);      System.exit(0);
     }
 的for(int i = 0; I< N;我++){
  通过out.println(用户名:+ angajati.get(I).username +密码+ angajati.get(我)。密码); }
  }
//我在这里特林得到的ArrayList出来的文件公共静态无效RdAng(字符串ARGS []){ ArrayList的< ContAngajat> angajati =新的ArrayList< ContAngajat>(50);
 ContAngajat续=新ContAngajat();
 诠释计数,I2,I; 尝试{
   FR的FileReader =新的FileReader(ConturiAngajati.txt);
   BR的BufferedReader =新的BufferedReader(FR);   串线=;
   而((行= br.readLine())!= NULL){   的String [] = theline line.split(|);
   数= theline.length;   对于(i = 0; I< theline.length;我++){   的String [] = theword theline [I] .split();
  }   }   对于(I2 = 0; I2<计数; I2 ++){   ContAngajat contrd =新ContAngajat();//ERROR在这里 对(INT IRD = 0; IRD&下; theword.length; IRD ++){ cont.username = theword [0];
     cont.password = theword [1];//他们都跟我theword解决不了每当我尝试运行此}
       angajati.add(contrd);
 } }
 赶上(IOException异常前){      的System.out.println(无法读取到文件);      System.exit(0);
     }
}
}

编译错误是 theword无法解析


解决方案

  

他们不断告诉我theword解决不了每当我试图运行此


这意味着 theword 的范围不声明。你不能访问它来调用任何方法。的 <他们/ em>的有它的权利。需要声明 theword 在更广的范围内,或移动code依赖于 theword 进入范围在那里被宣布。也许你已经宣布它内部的如果,而块左右,你要使用它的的在那里被宣布块。这是行不通的。

更详细的解答,可给予当你清理code,使其更好的可读性。

I need some help reading data from a text file into my ArrayList. The first part with the creating and putting the ArrayList into the text file works perfectly. I just need some help at the end in the "marked" area.

Note that some identifiers are in my native language.

public class ContAngajat {   
   String username;
   String password;
}

public class CreazaCont {

// creating the arraylist and putting it into a file 

public static void  ang(String args[])   { 

    ArrayList<ContAngajat> angajati=new ArrayList<ContAngajat>(50);

 Scanner diskScanner = new Scanner(in);

 Scanner forn = new Scanner(in);


 int n;

     out.print("Introduceti numarul de conturi noi care doriti sa le introduceti: ");
     n=forn.nextInt();
  out.println();

     try{

    FileWriter fw = new FileWriter("ConturiAngajati.txt", true);


    for(int i=0; i<n; i++){
  ContAngajat cont = new ContAngajat();

  out.print("Username: ");
  cont.username=diskScanner.nextLine();


  out.print("Password: ");
  cont.password=diskScanner.nextLine();

  angajati.add(cont);

  fw.write(cont.username + " ");
  fw.write(cont.password +"|");


  }
  fw.close();
     }
     catch(IOException ex){

      System.out.println("Could not write to file");

      System.exit(0);
     }




 for (int i=0; i<n; i++) {
  out.println("username: " + angajati.get(i).username + "  password: " +angajati.get(i).password );

 }


  }


// HERE I'M TRING TO GET THE ARRAYLIST OUT OF THE FILE

public static void  RdAng(String args[])   { 

 ArrayList<ContAngajat> angajati=new ArrayList<ContAngajat>(50);
 ContAngajat cont = new ContAngajat();
 int count,i2,i;

 try{


   FileReader fr = new FileReader("ConturiAngajati.txt");
   BufferedReader br = new BufferedReader(fr);

   String line = "";


   while((line=br.readLine())!=null) {

   String[] theline=line.split("|"); 
   count=theline.length;

   for(i=0;i<theline.length;i++) {

   String[] theword = theline[i].split(" "); 
  }     

   }   

   for(i2=0;i2<count;i2++)  {

   ContAngajat contrd = new ContAngajat();

// "ERROR" OVER HERE 

 for (int ird=0; ird <theword.length; ird++) {  

 cont.username=theword[0];
     cont.password=theword[1];

// they keep telling me "theword cannot be resolved" whenever i try to run this

}
       angajati.add(contrd); 
 }

 } 


 catch(IOException ex){

      System.out.println("Could not read to file");

      System.exit(0);
     }
}


}

The compilation error is theword cannot be resolved.

解决方案

they keep telling me "theword cannot be resolved" whenever I try to run this

This means that theword is not declared in the scope. You cannot access it to invoke any methods. They have it right. You need to declare theword in a broader scope, or to move the code which depends on theword into the scope where it is been declared. Maybe you've declared it inside an if or while block or so and you're trying to use it outside the block where it is been declared. That ain't going to work.

A more detailed answer may be given whenever you clean up the code so that it's better readable.

这篇关于如何从文件中读取数据到Java数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆