Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9182813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:39:03+00:00 2026-06-17T18:39:03+00:00

This is a java assignment containing 3 class files. The problem is when I

  • 0

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.)).

Can anyone see where I am going wrong? Thanks.

TV class file.

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

Main function

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:39:05+00:00Added an answer on June 17, 2026 at 6:39 pm

    You forgot to put this
    String opa = scan.next(); inside of while loop. You need to store the result of course, on each token.

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class assignment to read in data using Scanner. import java.util.Scanner; public
For a Java class assignment I was given a file named LogReader.class to use.
I have created a method in my java assignment to write into a file
I am doing this Java assignment for hours and stuck with this tester class
Hopefully someone knows the answer to this Java-Certification question: public static void main(String[] args)
Wrote up a basic file handler for a Java Homework assignment, and when I
This maybe a related question: Java assignment issues - Is this atomic? I have
i'm trying to read long types from a text file with using readLine() method
Working on my assignment for Java and I have created a class called Triangle.
Hi im doing a school assignment, and I need to convert this JAVA code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.