Note that there are some explanatory texts on larger screens.

plurals
  1. POMethod in main wont call method of a object instance?
    text
    copied!<p>This is a java assignment containing 3 class files. The problem is when I call the "doOperations" method in main. The if statements read the file correctly, but the object methods dont work(e.g. tv.on, tv.volumeUp tv.setChannel(scan.nextInt(), etc.)).</p> <p>Can anyone see where I am going wrong? Thanks.</p> <p>TV class file.</p> <pre><code>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 &lt; 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()); } } </code></pre> <p>Main function</p> <pre><code>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(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload