2013-01-24 26 views
0

這是一個包含3個類文件的java任務。問題是當我調用main中的「doOperations」方法時。 if語句正確讀取文件,但對象方法不起作用(例如,tv.on,tv.volumeUp tv.setChannel(scan.nextInt()等))。主方法不會調用對象實例的方法嗎?

任何人都可以看到我要去哪裏錯了嗎?謝謝。

電視類文件。

import java.io.*; 
import java.util.Scanner; 

public class TV { 
    boolean on; 
    boolean subscribe; 
    int currentchannel; 
    int indexChan; 
    int volume; 
    File chanList; 
    Scanner chanScan; 
    TVChannel[] channels; 
    final int MaxChannel = 100; 

    public TV(){ 
     on = false; 
     currentchannel = 1; 
     volume = 1; 
     channels = new TVChannel[MaxChannel]; //Create object of an array, default value = null. 
     subscribe = false; 

    } 

    public void turnOn(){ 
     on = true; 
    } 
    public void turnOff(){ 
     on = false; 
    } 
    public void channelUp(){ 
     currentchannel++; 
    } 
    public void channelDown(){ 
     currentchannel--; 
    } 
    public void volumeUp(){ 
     volume++; 
    } 
    public void volumeDown(){ 
     volume--; 
    } 
    public TVChannel getChannel(){ 
     return channels[currentchannel]; 
    } 
    public void setChannel(int c){ 
     if(channels[c]!= null) 
     currentchannel = c; 
    } 
    public boolean subscribe(String provider) throws IOException{ 
     chanList = new File(provider); 
     chanScan = new Scanner(chanList); 

     if(chanList.exists()){ 
      while(chanScan.hasNext()){ 

       indexChan = chanScan.nextInt(); 
       channels[indexChan] = new TVChannel(indexChan, chanScan.nextLine()); 

      } 
      chanScan.close(); 
      return true; 
     } 
     else return false; 
    } 

    public void printAll(){ 
     for(int i = 0; i < MaxChannel; i++){ 

      if(channels[i]!= null) 
      System.out.println(channels[i].channelNumber+"\t"+channels[i].channelName); 
     } 
    } 

    public void displayChannel(int c){ 
     if(channels[c]!= null) 
     System.out.println(channels[c].getChannel()+"\t"+channels[c].getName()); 
    } 
    public void displayCurrentChannel(){ 
     if(channels[currentchannel] != null) 
     System.out.println(channels[currentchannel].getChannel() +" "+ channels[currentchannel].getName()); 
    } 
} 

主要功能

import java.io.File; 
import java.io.IOException; 
import java.util.Scanner; 


public class TestTV { 

    public static void main(String[] args) throws IOException{ 


     TV tv = new TV(); 
     tv.subscribe("chan.txt"); 

     if(tv.subscribe = true){ 
      tv.printAll(); 
      doOperations("operations.txt", tv); 

     } 

    } 
    //Static means only one instance of object. 
    public static void doOperations(String opFileName, TV tv) throws IOException{ 
      File op = new File(opFileName); 
      Scanner scan = new Scanner(op); 
      String opa = scan.next(); 

      while(scan.hasNext()){ 
       System.out.println(scan.next()); 

       if(opa.equals("on")) tv.turnOn(); 
       else if(opa.equals("off")) tv.turnOff(); 
       else if(opa.equals("channelUp")) tv.channelUp(); 
       else if(opa.equals("channelDown")) tv.channelDown(); 
       else if(opa.equals("volumeUp")) tv.volumeUp(); 
       else if(opa.equals("volumeDown")) tv.volumeDown(); 
       else if(opa.equals("showChannel")) tv.displayCurrentChannel(); 
       else if(opa.equals("setChannel"))tv.setChannel(scan.nextInt()); //Error 
      } 
      scan.close(); 
     } 

} 
+0

儘管你現在有答案 - 你可能會通過逐步調試代碼的步驟很容易發現問題:你會意識到,同樣'其他if'聲明總是被執行。 – assylias

+0

是的,assylias知道什麼是好的。 讓調試器成爲你的第一手幫助,你會看到結果;) – nullpotent

回答

1

你忘了把這個 String opa = scan.next(); while循環的內部。您需要將結果存儲在每個令牌上。

while(scan.hasNext()){ 
    opa = scan.next(); 
    System.out.println(opa); //don't just call scan.next() 
          //and discard the result. Unless that's really what you want? 
    ..... 
} 
0

您的代碼沒有問題。 確保您operation.txt看起來像這樣 showChannel setChannel 2

注:因爲你是scan.next();在while循環之前,再次在循環中作爲System.out.println(scan.next()); 您將忽略operation.txt的第一行,您會看到它正確設置通道。

建議: 更改它作爲

 String opa= "";// = scan.next(); 
     while(scan.hasNext()){ 
      System.out.println(scan.next()); 
+0

好吧,這很有意義,我剛剛開始掌握如何使用不同的類以及如何掃描文件。 – ubunix

+0

我很感謝評論,謝謝 – ubunix