java - 将包含 boolean 值的文本文件作为对象读取到数组列表中

标签 java arraylist interface boolean

我正在尝试将字符串 int 和 boolean 值的文件作为对象 block 读取到数组列表中。字符串值进入数组列表就好了,它是我遇到问题的 boolean 值。每次我遇到变量“active”时,都会出现不匹配异常。请帮忙!如果该 block 是向导,则文本文件按此顺序排列 名称(字符串) 位置(字符串) active (boolean) ...我遇到问题的那个 技能等级(int) 友好度(int)

我包含了驱动程序类以及 Witch 类,其中包含 变量“active”最初。

enter image description here

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>Driver class that adds objects to the array list based on what the scanner 
reads from the file
    package project2;

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;

    public class Project2 {

      public static void main(String[] args) {

        Scanner inputFileScanner1 = null;

        //file name
        String listFile = "list.txt";

        // Check to see if file exists
        try {
          inputFileScanner1 = new Scanner(new File(listFile));

        } catch (FileNotFoundException e) {

          System.out.println("Error opening file.");
          System.exit(1);
        }

        //create Individuals arraylist and Location arraylist
        ArrayList < Individual > Individual = new ArrayList < > ();
        ArrayList < String > Location = new ArrayList < > ();

        //declare variables to read file contents into the arraylist
        String wizName, witchName, individualName, location, position, 

        profession = null, line = null;
        int wizLevel, witchSkillLevel, friendliness;
        boolean active;

        //while there is a next line, if the line equals Wizard, the next                                        four lines
        // are wizard name, location, position and level
        while (inputFileScanner1.hasNext()) {
          line = inputFileScanner1.nextLine();
          if (line.trim().equals("Wizard")) {

            wizName = inputFileScanner1.nextLine().trim();
            location = inputFileScanner1.nextLine().trim();
            position = inputFileScanner1.nextLine().trim();
            wizLevel = inputFileScanner1.nextInt();

            //create wizard object
            Individual wizard = new Wizard(wizName, location, position, profession, wizLevel);

            //fill arraylist with wizard objects
            Individual.add(wizard);
            Location.add(location);

          } //if the next line is Witch, the next five lines are
          // witch name, location, yes/no active, skill level, and friendliness
          //in that order
          else if (line.trim().equals("Witch")) {
            witchName = inputFileScanner1.nextLine().trim();
            location = inputFileScanner1.nextLine().trim();
            active = inputFileScanner1.nextBoolean();
            witchSkillLevel = inputFileScanner1.nextInt();
            friendliness = inputFileScanner1.nextInt();

            //create witch object
            Individual witch = new Witch(witchName, location, profession, witchSkillLevel, friendliness, active);

            //fill the arraylist with witch objects
            Individual.add(witch);
            Location.add(location);
          } else {

            profession = line.trim();
            individualName = inputFileScanner1.nextLine().trim();
            location = inputFileScanner1.nextLine().trim();

            Individual i = new Individual(profession, individualName, location);

            Individual.add(i);
            Location.add(location);
          }
          java.util.Collections.sort(Individual);
          java.util.Collections.sort(Location);

        }

        System.out.println("List of friends and possible allies: " + Location);

        inputFileScanner1.close();

      }

    }</code></pre>
</div>
</div>

//保存文本文件中的值的 Witch 类。 active 是我遇到问题的 boolean 值 打包项目2;

 public class Witch extends Individual implements Magical {

      private int skill;
      private int friendly;

          //Constructor with witch parameters
      public Witch(String name, String location, String profession,
        int skill, int friendly, boolean active) {

      }

      //default constructor
      public Witch() {

      this("", "", "", 0, 0, false);
      }

      //overridden abstract method from magical interface 
      @Override
      public void assess() {
        System.out.print(this.friendly + " " + this.skill + " " + super.toString());
      }
    }

<!-- end snippet -->

文本文件: enter image description here

最佳答案

当您拉入 boolean 变量时,请执行以下操作。

if(inputFileScanner1.nextLine().trim().equals("yes"))
{
    active = true;
}
else
{
    active = false;
}

关于java - 将包含 boolean 值的文本文件作为对象读取到数组列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42323820/

相关文章:

java - Java 中的接口(interface)和抽象类

c# - 如何在注入(inject)的类上调用泛型方法而不通知泛型类型的父类?

c# - 可变类型的不可变 View

Netbeans 中的 Java 数据库连接

android - 转换为 ArrayList<?在 onSaveInstanceState 中保存 List 时扩展 Parcelable>

java - 我的对象值不会保存。显示空

java - 如何从存储在数组列表中的对象中提取字段?

java - 如何在 ExpandableListView 中的组上设置 LongClick [Android]

java - Spring Boot Actuator 添加 X-Frame-Options = DENY 到所有端点(特别是错误端点)

java - MessageDigest 的 update 方法有什么作用?BASE64Encoder 的作用是什么?