0

I need to create a method for my Television class that will receive an integer, but will return a string value (channel name) from an array. I am having trouble in the toString() are where I am receiving an error "error- '.class' expected' for the part that is supposed to display the channel name. Thanks and here is my code.

public class Television

{

private int channel;
private int currentChannel;
private int volume;
private boolean power;
private String[] channelName = {"CBS", "FOX", "DISCOVERY", "PBS", "HBO", "CNN", "DISNEY", "CNN", "TBS", "USA"};



//No argument constructor
public Television()
{
    currentChannel = 1;
    volume = 0;
    power = false;

}


public void powerChange()
{
    this.power = !this.power;
}

public void setVolume(int vol)
{
    if (vol>10)
    {
    volume = 10;
    }else
    {
        volume = vol;
    }
    if (vol<0)
    {
    volume = 0;
    }

}

public void increaseVolume()
{
    volume++;
}

public void decreaseVolume()
{
    volume--;
}

public int getVolume()
{
    return volume;
}

public void setChannel(int ch)
{
    if(ch>10)
    {
    channel = 10;
    }else
    {
        channel = ch;
    }
    if(ch<1)
    {
    channel = 1;
    }
}

public void increaseChannel()
{
    channel++;
}

public void decreaseChannel()
{
    channel--;
}

public int getChannel()
{
    return channel;
}

public String getChannelName(int channel)
{

    if (channel==1)
    {
        return channelName[0];
    }
    else if (channel == 2)
    {
        return channelName[1];
    }   
    else if (channel == 3)
    {
        return channelName[2];
    }   
    else if (channel == 4)
    {
        return channelName[3];
    }   
    else if (channel == 5)
    {
        return channelName[4];
    }   
    else if (channel == 6)
    {
        return channelName[5];
    }   
    else if (channel == 7)
    {
        return channelName[6];
    }
    else if (channel == 8)
    {
        return channelName[7];
    }   
    else if (channel == 9)
    {
        return channelName[8];
    }   
    else if (channel == 10)
    {
        return channelName[9];
    }   


}


public String toString()
{
    if(!power)
    {
        return String.format("%s :%s\n%s :%d\n %s :%s\n%s :%d", "TV State", "OFF", "Channel No", channel, "Channel Name", getChannelName(channel), "Volume", volume);
    }
    else if(power)
    {
        return String.format("%s :%s\n%s :%d\n %s :%s\n%s :%d", "TV State", "ON", "Channel No", channel, "Channel Name", getChannelName(channel), "Volume", volume);
    }


}

}

2
  • What do you think this piece of code does: getchannelName[]? Commented Nov 12, 2013 at 3:10
  • I did not have getchannelName[] in my original code, but I was trying to play with the toString() just to see if anything would help it. I was trying to invoke the method I had created earlier but I realize that this did not work. Commented Nov 12, 2013 at 3:18

1 Answer 1

2
TVChannel[] channelName = {"CBS", "FOX", "DISCOVERY", "PBS", "HBO", 
                           "CNN", "DISNEY", "CNN", "TBS", "USA"};

Should be

String[] channelName = {"CBS", "FOX", "DISCOVERY", "PBS", "HBO", 
                        "CNN", "DISNEY", "CNN", "TBS", "USA"};

Your array is of TVChannel type and should be of String type. TVChannel is a class that does not even exist.

Edit: also consider

channelName = new TVChannel[MaxChannel];  // delete this from your constructor.
                                          // it is not needed. Your instance of
                                          // the class already give you array

Also consider your getChannelName() method. Maybe you swant take an int argument?

public String getChannelName(int channel){ // instead of using the instance var
                                           // the logic really doesn't make sense
                                           // for your situation

}
Sign up to request clarification or add additional context in comments.

10 Comments

Or it needs to list instances of TVChannel, which ever the OP actually needs ;)
Thanks. I have changed this but I am still receiving the same error message.
@user2981579, you don't need this piece of code in your constructor channelName = new TVChannel[MaxChannel];
@peeskillet thank you, that does make more sense. Any tips on how to refer to it in the toString method? I am still getting and error from this.
@user2981579 getchannelName[]. You want getChannel(channel) in your toString(). What error are you getting? Is it just in the toString()?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.