2013-07-12 87 views
1
import java.io.*; 
import java.util.ArrayList; 
import java.util.StringTokenizer; 

public class FileReader 
{ 
    public static final String PATH_TO_DATA_FILE = "playtennis.data"; 
    public static ArrayList<Record> buildRecords() { 
    BufferedReader reader = null; 
    DataInputStream dis = null; 
    ArrayList<Record> records = new ArrayList<Record>(); 

    try { 
     File f = new File(PATH_TO_DATA_FILE); 
     FileInputStream fis = new FileInputStream(f); 
     reader = new BufferedReader(new InputStreamReader(fis));; 

     // read the first record of the file 
     String line; 
     Record r = null; 
     ArrayLAist<DiscreteAttribute> attributes; 
     while ((line = reader.readLine()) != null) { 
      StringTokenizer st = new StringTokenizer(line, ","); 
      attributes = new ArrayList<DiscreteAttribute>(); 
      r = new Record(); 

      if(Hw1.NUM_ATTRS != st.countTokens()) { 
       throw new Exception("Unknown number of attributes!"); 
      } 

      @SuppressWarnings("unused") 
      String day = st.nextToken(); 
      String outlook = st.nextToken(); 
      String temperature = st.nextToken(); 
      String humidity = st.nextToken(); 
      String wind = st.nextToken(); 
      String playTennis = st.nextToken(); 

      if(outlook.equalsIgnoreCase("overcast")) { 
       attributes.add(new DiscreteAttribute("Outlook", DiscreteAttribute.Overcast)); 
      } 
      else if(outlook.equalsIgnoreCase("sunny")) { 
       attributes.add(new DiscreteAttribute("Outlook", DiscreteAttribute.Sunny)); 
      } 
      else if(outlook.equalsIgnoreCase("rain")) { 
       attributes.add(new DiscreteAttribute("Outlook", DiscreteAttribute.Rain)); 
      } 

      if(temperature.equalsIgnoreCase("hot")) { 
       attributes.add(new DiscreteAttribute("Temperature", DiscreteAttribute.Hot)); 
      } 
      else if(temperature.equalsIgnoreCase("mild")) { 
       attributes.add(new DiscreteAttribute("Temperature", DiscreteAttribute.Mild)); 
      } 
      else if(temperature.equalsIgnoreCase("cool")) { 
       attributes.add(new DiscreteAttribute("Temperature", DiscreteAttribute.Cool)); 
      } 

      if(humidity.equalsIgnoreCase("high")) { 
       attributes.add(new DiscreteAttribute("Humidity", DiscreteAttribute.High)); 
      } 
      else if(humidity.equalsIgnoreCase("normal")) { 
       attributes.add(new DiscreteAttribute("Humidity", DiscreteAttribute.Normal)); 
      } 

      if(wind.equalsIgnoreCase("weak")) { 
       attributes.add(new DiscreteAttribute("Wind", DiscreteAttribute.Weak)); 
      } 
      else if(wind.equalsIgnoreCase("strong")) { 
       attributes.add(new DiscreteAttribute("Wind", DiscreteAttribute.Strong)); 
      } 

      if(playTennis.equalsIgnoreCase("no")) { 
       attributes.add(new DiscreteAttribute("PlayTennis", DiscreteAttribute.PlayNo)); 
      } 
      else if(playTennis.equalsIgnoreCase("yes")) { 
       attributes.add(new DiscreteAttribute("PlayTennis", DiscreteAttribute.PlayYes)); 
      } 

      r.setAttributes(attributes); 
      records.add(r); 
     } 
    } 
} 

我還沒有給出文件名的FileReader錯誤無法找到符號和類離散屬性

  1. 我得到的錯誤,無法歸檔符號
  2. 找不到符號符號:類離散屬性,位置:類classifier.FileReader
  3. 找不到符號符號:變量離散屬性的位置:類classifier.FileReader
+0

您需要爲DiscreteAttribute類添加導入。 –

+0

導入那個類:'import package.DiscreteAttribute'!並且還改變它與'java.io.FileReader'衝突的類的名字! – NINCOMPOOP

回答

0

Here DiscreteAttribute類。你需要添加你的項目類。

public class DiscreteAttribute extends Attribute { public static final int Sunny = 0; public static final int Overcast = 1; public static final int Rain = 2; public static final int Hot = 0; public static final int Mild = 1; public static final int Cool = 2; public static final int High = 0; public static final int Normal = 1; public static final int Weak = 0; public static final int Strong = 1; public static final int PlayNo = 0; public static final int PlayYes = 1; enum PlayTennis { No, Yes } enum Wind { Weak, Strong } enum Humidity { High, Normal } enum Temp { Hot, Mild, Cool } enum Outlook { Sunny, Overcast, Rain } public DiscreteAttribute(String name, double value) { super(name, value); } public DiscreteAttribute(String name, String value) { super(name, value); } } 
相關問題