Okay I am working on arrays in class this week and since it is an online course I don't have anyone to turn to. YAY me! Basically this is what I have been asked to do.


Create a class named TVShow.java. Data Fields can include a String containing the Name of the show, a String containing the day that the show is broadcast(i.e. “Monday”) and an integer for the channel where the show can be viewed. Methods will include a constructor that requires values for all of the data fields, plus methods to get, set and display the values of the data fields. Create a UseTVShow.java file that prompts the user to input up to 5 TV shows and stores the data in an array of objects first, then displays them as a list.

For the TVShow.java class I have this:

import javax.swing.*;
public class TVShow 
{
    private int tvChannel = 0;
    private String showDay = "";
    private String showName = "";
    
    public TVShow(String name, String day, int channel)
    {
        showName = name;
        showDay = day;
        tvChannel = channel;
    }
    
    public void setTVChannel(int theChannel)
    {
        tvChannel = theChannel;
    }
    public int getTVChannel()
    {
        return tvChannel;
    }
    public void setDay(String weekDay)
    {
        showDay = weekDay;
    }
    public String getDay()
    {
        return showDay;
    }
    public void setShowName(String nameOfShow)
    {
        showName = nameOfShow;
    }
    public String getShowName()
    {
        return showName;
    }
    
    public void setDisplay()
    {
        JOptionPane.showMessageDialog(null, showName + " is on " + showDay + "'s, " 
                + "on channel " + tvChannel);
    }    
}

For the Main method in the useTVShow.java I have this:

import javax.swing.*;
import java.util.*;

public class UseTVShow 
{
    public static void main(String[] args)
    {
        final int TV_MAX = 5;
        TVShow[] myShowArray = new TVShow[TV_MAX];
        String tvInput = JOptionPane.showInputDialog(null, "Please enter a television show: ");
        for(int d = 0; d < TV_MAX; ++d)
            myShowArray[d].setShowName(tvInput);
        for(int d = 0; d < TV_MAX; ++d)
            myShowArray[d].setDay("Monday");
        for(int d = 0; d < TV_MAX; ++d)
            myShowArray[d].setTVChannel(1 + d);
        for(int d = 0; d < TV_MAX; ++d)
        JOptionPane.showMessageDialog(null, "Show: " + d + " " + myShowArray[d].getShowName() 
                + " airs on " + myShowArray[d].getDay() + "'s on channel " 
                + myShowArray[d].getTVChannel());
    }
}

I am having two issues here, first: I am unable to enter more than 1 television show. Second: I am unable to display what I have entered or even the other variables from the get and set methods. HELP!

Recommended Answers

All 11 Replies

Update to the code on the Main method useTVShow.java

import javax.swing.*;

public class UseTVShow 
{
    public static void main(String[] args)
    {
        final int TV_MAX = 5;
        int d;
        TVShow[] myShowArray = new TVShow[TV_MAX];
        String tvInput = "";
        for(d = 0; d < TV_MAX; ++d)
            tvInput = JOptionPane.showInputDialog(null, "Please enter a television show: ");
            myShowArray[d].setShowName(tvInput);
            myShowArray[d].setDay("Monday");
            myShowArray[d].setTVChannel(1 + d);
        JOptionPane.showMessageDialog(null, "Show: " + myShowArray[d].getShowName() 
                + " airs on " + myShowArray[d].getDay() + "'s on channel " 
                + myShowArray[d].getTVChannel());
    }
}

I am unable to enter more than 1 television show.

What happens after the first show is entered? The code looks like it is in a loop and should continue.
Add a println to show the value of d inside of the loop as the loop goes around.

the modifications I made to the Main method, have allowed me now to enter more than 1 TV show, but even when using the System.out.println(text here blah blah blah); method I am unable to see what has been stored in the array.

What I did was removed the additional

for(int d = 0; d < TV_MAX; ++d)

loops and then moved the JOptionPane line into the loop. So it allows me now to enter the television shows 5 different times. What is happening now though is they are being stored in the array I created in the TVShow.java class and I do not know how to make them display even with the println method. What I need to know is how to call the

public void setDisplay()
    {
        JOptionPane.showMessageDialog(null, showName + " is on " + showDay + "'s, " 
                + "on channel " + tvChannel);
    }

from the TVShow.java class into the main method and display it. Does that make sense?

I am unable to see what has been stored in the array.

What does print out? You should post the output, I can't see your PC's monitor from here.

Post your new code so we can see how you are trying to print. Do you use one of the TVShow's methods to get to the contents of the array elements?

import javax.swing.*;

public class UseTVShow 
{
    public static void main(String[] args)
    {
        final int TV_MAX = 5;
        int d;
        TVShow[] myShowArray = new TVShow[TV_MAX];
        String tvInput = "";
        for(d = 0; d < TV_MAX; ++d)
            tvInput = JOptionPane.showInputDialog(null, "Please enter a television show: ");
            myShowArray[d].setShowName(tvInput);
            myShowArray[d].setDay("Monday");
            myShowArray[d].setTVChannel(1 + d);
        JOptionPane.showMessageDialog(null, "Show: " + myShowArray[d].getShowName() 
                + " airs on " + myShowArray[d].getDay() + "'s on channel " 
                + myShowArray[d].getTVChannel());
    }
}

This is what I have written to try and display the data. Specifically the

JOptionPane.showMessageDialog(null, "Show: " + myShowArray[d].getShowName() 
                + " airs on " + myShowArray[d].getDay() + "'s on channel " 
                + myShowArray[d].getTVChannel());

section of the code. Any thoughts?

What do lines 16-18 show?
Add a println next to those lines that prints the same Strings on the console and then copy and paste the output here.

That's it for tonight. Back tomorrow.

run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
	at UseTVShow.main(UseTVShow.java:16)
Java Result: 1
BUILD SUCCESSFUL (total time: 6 seconds)

This is the output I am receiving, though I have to be honest I don't understand what it means.

(Norm: am I going bonkers here? Why no NPE's?)

TVShow[] myShowArray = new TVShow[TV_MAX];
..
for(d = 0; d < TV_MAX; ++d)
..
  myShowArray[d].setShowName(tvInput);

This shouldn't work. Have you been ignoring null pointer error messages?
Line 1 creates an array of TVShow pointers, initially all null
Line 3 tries to call a method on an array element BUT the array still just contains null pointers. This should throw an Exception.
You need to add a new TVShow to each array elements before using it, as in

TVShow[] myShowArray = new TVShow[TV_MAX];
...
for(d = 0; d < TV_MAX; ++d)
   tvInput = JOptionPane.showInputDialog(null, "Please enter a television show: ");
   myShowArray[d] = new TVShow(...);  
   myShowArray[d].setShowName(tvInput);

Yes, the posted code should get a NPE.
So if the OP isn't getting one, does he have different code than what is posted?

One thing the OP hasn't talked about is that the code asks 5 times for input and never puts the input anywhere because the for loop is one statement long. Maybe he quits the program before he gets past the data entry loop.

Thanks! That did it. It appears I also had initiated

private int tvChannel = 0;
    private String showDay = "";
    private String showName = "";

this twice. Once I removed the extra code and added the

myShowArray[d] = new TVShow(...);

line it works wonderfully! Thank you so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.