将具有布尔值的文本文件作为对象读入数组列表

时间:2017-02-19 05:00:52

标签: java arraylist interface boolean

我正在尝试将字符串int和布尔值的文件作为对象块读入数组列表中。字符串值很好地进入数组列表,它的布尔值我遇到了麻烦。每当我遇到变量' active'就会出现不匹配异常。请帮忙!如果块是向导的文本文件按此顺序排列 名字(字符串) 位置(字符串) active(boolean)...我遇到问题的那个 技能水平(int) 友善(int)

我包含了驱动程序类以及包含该类的Witch类 变量'活跃'原本。

enter image description here

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

      }

    }

//包含文本文件中值的Witch类。 active是布尔值我遇到了麻烦     package project2;

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

      }

    }

文字档案: enter image description here

2 个答案:

答案 0 :(得分:0)

当你拉入你的布尔变量时,做这样的事情。

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

答案 1 :(得分:0)

好的,问题是该文件包含字符串yesno,这些字符串无法直接解析为布尔值(应为truefalse)。< / p>

如果你能以某种方式更改原始数据文件,我建议使用两个truefalse关键字,否则@Sendrick Jefferson解决方案将完成这项工作(风险自负:每一个错字,例如&#34; ye&#34;,将被翻译成错误的。)