2017-02-19 8 views
1

文字列intとboolean値のファイルをオブジェクトリストとして配列リストに読み込もうとしています。文字列の値は配列のリストに入っています。それは私が問題を抱えているブール値です。私が変数 'active'に遭遇するたびに、ミスマッチ例外があります。助けてください!ブロックがウィザードの場合のテキストファイルは、この順番になります。 名前(文字列) 場所(文字列) アクティブ(ブール値)... スキルレベル(int) フレンドリー)ブール値を持つテキストファイルをオブジェクトとして配列リストに読み込む

私はドライバクラスと、もともと変数 'active'を含む を含む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(); 
 

 
     } 
 

 
    }

//魔女クラス。 activeは、 package project2で問題があるブール値Imです。

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

+0

システムが読み込んでいるファイルの一部を追加できますか? – Enkk

+0

ファイルの内容のイメージを追加しました。フォーマットはウィザード(キーワード)、フルネーム、場所、位置、レベル...魔女(キーワード)、氏名、場所、アクティブ(ブール値)、スキルレベル、フレンドリー...他の職業、氏名、所在地 – shatown

答えて

0

あなたはブール変数に引っ張る。このような何かを行います。

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

ありがとうセドリック! – shatown

+0

答えが正しい場合は、下の記号の下にあるチェックマークをクリックします。 – Sedrick

0

さて、問題は、ファイルがブール(trueまたはfalseする必要があります)などを直接解析可能でない文字列yesnoが、含まれていることです。

あなたは何とか元のデータファイルを変更することができる場合、私はそれ以外の場合は、@Sendrickジェファーソン・ソリューションは、ご自身の責任で(仕事をする、2つのtruefalseキーワードを使用することをお勧め: "インスタンスの場合と同様に、すべてのタイプミスあなたがたは、偽になるであろう)。

+0

ええ、私たちはファイルを変更することはできません:(しかし、助けをありがとう! – shatown

関連する問題