当前位置: 首页 > 面试题库 >

如何从文件中读取数据到Java中的数组?

杜霍英
2023-03-14
问题内容

我需要一些帮助,将数据从文本文件读入我的ArrayList。创建和将其ArrayList放入文本文件的第一部分可以很好地工作。最后,我只需要在“已标记”区域中提供一些帮助。

请注意,某些标识符使用我的母语。

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


}

编译错误为theword cannot be resolved


问题答案:

每当我尝试运行此命令时,他们都会不断告诉我“无法解决该问题”

这意味着theword未在范围中声明。您不能访问它来调用任何方法。 他们
说对了。您需要theword在更大的范围内进行声明,或者将依赖于此的代码移到theword声明它的范围内。也许您已经在ifwhile块内声明了它,并且试图在声明它的块
使用它。那是行不通的。

每当您清理代码时,可能都会给出更详细的答案,以使代码更易读。



 类似资料:
  • 问题内容: 我有以下格式的文本文件: Details.txt 该文件是.txt文件。我想从该文件中读取课程标题,并打印相应的教科书和教师信息。但是我不确定该遵循什么程序?将信息存储在数组中效率不高!我应该如何进行?注意:我无法更改文件中的信息,因此不应更改!显然,文件将通过以下代码读取: 但是我应该如何根据课程名称,教科书和讲师的标签从该文件中提取数据! 问题答案: 首先正确地逐行阅读文件,然后搜

  • 问题内容: 我已经搜索了stackoverflow,但是没有找到明确的答案。如何将数据从XLS文件的特定行和列读取到我的Android应用程序?如何读取XLS文件?我不想将其转换为CSV,因为在尝试将其转换时出现错误。 也许我可以使用此http://www.andykhan.com/jexcelapi/tutorial.html#reading,但我什至不知道如何将其导入到我的项目中。请帮忙。 问

  • 我有一个非常简单的问题:使用Python从txt文件中读取不同条目的最有效方法是什么? 假设我有一个文本文件,如下所示: 在C中,我会这样做: 用Python做这样的事情最好的方法是什么?以便将每个值存储到不同的变量中(因为我必须在整个代码中使用这些变量)。 提前感谢!

  • 问题内容: 我有一个包含一些元数据的文件,然后是包含2个带有标题的列的实际数据。在numpy中使用genfromtxt之前,是否需要将两种类型的数据分开?还是可以以某种方式拆分数据?将文件指针放在标题上方行的末尾,然后从那里尝试genfromtxt怎么办?谢谢该文件的格式如下所示: 问题答案: 如果您不希望第一行,请尝试(如果没有丢失的数据): 或(如果缺少数据): 如果然后要解析标头信息,则可以

  • 问题内容: Hai,我是Angularjs的新手。我正在尝试从JSON文件读取数据,但是它返回了奇怪的输出。这是我的controller.js文件 这是我的services.js文件 控制台输出为 请帮忙。 问题答案: $ http.get 返回一个promise,您将返回一个包含该promise的数组。 而是这样做: 然后以这种方式使用您的工厂:

  • 必须从文档中检索突出显示的数据。 这是我用来返回页面中的数据的方法,但没有得到用黄色标记的数据,只有那些数据被输入到输出中,而这些数据在word文档的表中没有提到。