import java.util.Scanner;


class TestTelevisionV3
{
public static void main(String [] args)
{
Television tv = new Television (4, 8, true);
tv.printImage();
System.out.println("");
System.out.println("Press P to turn Power On or Off");
System.out.println("Press C to Change the Channel");
System.out.println("Press V to Change the Volume");
System.out.println("Press Q to quit");
System.out.println("");


Scanner myInput = new Scanner(System.in);
String Input1 = myInput.next();


if(Input1 == "P")
{
tv.setPower();
}
if(Input1 == "C")
{
tv.setChannel();
}
if(Input1 == "V")
{
tv.setVolume();
}
if(Input1 == "Q")
{
tv.setPower();
}
else
{
System.out.println("");
System.out.println("Error - Input not recognised!");
System.out.println("");
}
}
}

I am a university students studying software development.
I have been given a task which involves using the code shown so the user inputs P C V or Q the following method is run.

However whenever i run the code my error message appears.

I am aware that the problems has something to do with the scanning method not being able to find string or character inputs..

can someone help me modify the scanner so that inputting P will run the tv.setPower Method

Don't worry everybody does that mistake. And I am tired of replying to it.

Just use the equals method.

When you use '==' to compare String (remember Strings are Objects) you compare to see if they are the SAME objects. They are not, but they have the same value:

if (Input1.equals("P")) {
 .....
}
String s1="a";
String s2="a"; //2 Different objects.

(s1==s2) //false
(s1.equals(s2)) //true

BUT

String s1 = "a";
String s2 = s1; //they are the same

(s1==s2) //true
(s1.equals(s2)) //true

Also you will need to have all of your IFs as 'else if':

if () {

} else if () {

} else if () {

} .... {

} else {
System.out.println("");
System.out.println("Error - Input not recognised!");
System.out.println("");
}
commented: the .equals method was very usefull in my studies +1

I have implemented what you have just stated but it hasnt solved my problem

Here is the code

import java.util.Scanner;


class TestTelevisionV3
{
public static void main(String [] args)
{
Television tv = new Television (4, 8, true);
tv.printImage();
System.out.println("");
System.out.println("Press P to turn Power On or Off");
System.out.println("Press C to Change the Channel");
System.out.println("Press V to Change the Volume");
System.out.println("Press Q to quit");
System.out.println("");


Scanner myInput = new Scanner(System.in);
String Input1 = myInput.next();


String s1="P";
String s2="C";
String s3="V";
String s4="Q";


if(Input1.equals (s1))
{
tv.setPower();
}
if(Input1.equals (s2))
{
tv.setChannel();
}
if(Input1.equals (s3))
{
tv.setVolume();
}
if(Input1.equals (s4))
{
tv.setPower();
}
else
{
System.out.println("");
System.out.println("Error - Input not recognised!");
System.out.println("");
}
}
}

Here is the output

Press P to turn Power On or Off
Press C to Change the Channel
Press V to Change the Volume
Press Q to quit


P

Error - Input not recognised!

it does work, but you didn't implement everything.
you programmed it so, that if Input1 contains the same value as s4, the error message is not displayed, otherwise it is.
just reread JavaAddict's post and put the else's where needed

ok thanks for that bit

however the inputs for C V and Q still dont work

also i have a problem with my setPower method which i hill need help with afterwards

define or clarify:

...do not work

show your code now, using Code-tags, and explain what it does wrong
that might give us some more to go on

import java.util.Scanner;

class TestTelevisionV3
{
    public static void main(String [] args)
    {
        Television tv = new Television (4, 8, true);

        tv.printImage();

        System.out.println("");
        System.out.println("Press P to turn Power On or Off");
        System.out.println("Press C to Change the Channel");
        System.out.println("Press V to Change the Volume");
        System.out.println("Press Q to quit");
        System.out.println("");

        Scanner myInput = new Scanner(System.in);
        String Input1 = myInput.next();

        String s1="P";
        String s2="C";
        String s3="V";
        String s4="Q";

        if(Input1.equals(s1))
        {
            tv.setPower();
            tv.printImage();
        }
        else if(Input1.equals(s2))
        {
            tv.setChannel();
            tv.printImage();
        }
        else if(Input1.equals(s3))
        {
            tv.setVolume();
            tv.printImage();
        }
        else if(Input1.equals(s4))
        {
            tv.setPower();
            tv.printImage();
        }
        else
        {
        System.out.println("");
        System.out.println("Error - Input not recognised!");
        System.out.println("");
        }
    }
}

ok the program allows me to enter "P" as the input

However it does not allow me to enter "C" "V" or "Q" as the input

[h8063984@linux15 Version3]$ java TestTelevisionV3
__________________________________
| ______________________________ |
| |Channel: 4                | |
| |Volume: 8                 | |
| |                            | |
| |                            | |
| |                            | |
| |                            | |
| |                            | |
| |____________________________| |
|               *                |
|________________________________|

Press P to turn Power On or Off
Press C to Change the Channel
Press V to Change the Volume
Press Q to quit

C
C
Exception in thread "main" java.util.InputMismatchException
        at java.util.Scanner.throwFor(Scanner.java:840)
        at java.util.Scanner.next(Scanner.java:1461)
        at java.util.Scanner.nextInt(Scanner.java:2091)
        at java.util.Scanner.nextInt(Scanner.java:2050)
        at Television.setChannel(television.java:28)
        at TestTelevisionV3.main(TestTelevisionV3.java:33)
[h8063984@linux15 Version3]$

that is the output which it gives me in the terminal. Note that i had to enter "C" twice

it will take a while to reply to any more replies since i am about top get on a bus to go home

I've tried to run your code ... it's working. Try to rebuild your project.

As the printStacktrace states:

...
at java.util.Scanner.nextInt(Scanner.java:2050)
at Television.setChannel(television.java:28)
...

There is an error at the method: setChannel of the Television class at line: 28. And the problem is with the way you use the nextInt method of the Scanner class. Better use the 'next' method and convert what you get to int.
It wasn't very difficult to figure that out, so post only the code of the Television class.

AND USE CODE TAGS. Click the button: '#' that is above where you post your reply

String s1="a";
String s2="a"; //2 Different objects.

(s1==s2) //false
(s1.equals(s2)) //true

Think again; hint: String pool.

thnx for the help with that problem

my problem now is that the

setPower()

method is not working

here is the

setPower()

methods code

void setPower()
    {
        if(power == true)
        {
            power = false;
        }
        if(power == false)
        {
            power = true;
        }
    }

and here is the output which is supposed to appear

if(power == false)
        {

            System.out.println("__________________________________");
            System.out.println("| ______________________________ |");
            System.out.println("| |                            | |");
            System.out.println("| |                            | |");
            System.out.println("| |                            | |");
            System.out.println("| |                            | |");
            System.out.println("| |                            | |");
            System.out.println("| |                            | |");
            System.out.println("| |                            | |");
            System.out.println("| |____________________________| |");
            System.out.println("|               O                |");
            System.out.println("|________________________________|");
        }

the power at the start of the code is true so when i run the getpower method the power should change to false and the above output should appear however i always get the output which i would get if power eqauls on, channel equals 4 and volume equals 8

Try this:

post only the code of the Television class.

import java.util.Scanner;

class Television
{
    int volume;
    int channel;
    boolean power;

    Television(int c, int v, boolean p)
    {
        channel = c;
        volume = v;
        power = p;
    }

    void printImage()
    {
        if(power == true)
        {
            if(channel > 9 && volume > 9)
            {
                System.out.println("__________________________________");
                System.out.println("| ______________________________ |");
                System.out.println("| |Channel: " + channel +"                | |");
                System.out.println("| |Volume: " + volume +"                 | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |____________________________| |");
                System.out.println("|               *                |");
                System.out.println("|________________________________|");
            }
            if(channel > 9 && volume <= 9)
            {
                System.out.println("__________________________________");
                System.out.println("| ______________________________ |");
                System.out.println("| |Channel: " + channel +"                 | |");
                System.out.println("| |Volume: " + volume +"                   | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |____________________________| |");
                System.out.println("|               *                |");
                System.out.println("|________________________________|");
            }
            if(volume > 9  && channel <= 9)
            {
                System.out.println("__________________________________");
                System.out.println("| ______________________________ |");
                System.out.println("| |Channel: " + channel +"                  | |");
                System.out.println("| |Volume: " + volume +"                  | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |____________________________| |");
                System.out.println("|               *                |");
                System.out.println("|________________________________|");
            }
            if(channel <= 9 && volume <= 9)
            {
                System.out.println("__________________________________");
                System.out.println("| ______________________________ |");
                System.out.println("| |Channel: " + channel +"                  | |");
                System.out.println("| |Volume: " + volume +"                   | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |                            | |");
                System.out.println("| |____________________________| |");
                System.out.println("|               *                |");
                System.out.println("|________________________________|");
            }
        }
        if(power == false)
        {

            System.out.println("__________________________________");
            System.out.println("| ______________________________ |");
            System.out.println("| |                            | |");
            System.out.println("| |                            | |");
            System.out.println("| |                            | |");
            System.out.println("| |                            | |");
            System.out.println("| |                            | |");
            System.out.println("| |                            | |");
            System.out.println("| |                            | |");
            System.out.println("| |____________________________| |");
            System.out.println("|               O                |");
            System.out.println("|________________________________|");
        }
    }

    int getChannel()
    {
        if(power == false)
        {
            channel = 0;
        }
        return channel;
    }

    void setChannel()
    {
        System.out.println("Please input channel number:");

        Scanner myInput = new Scanner(System.in);
        int c = myInput.nextInt();

        if(power == true)
        {
            if(c<=10 && c>= 0)
                {
                    channel = c;
                }
            else
                {
                    channel = 1;
                }
        }
    }

    int getVolume()
    {
        if(power == false)
        {
            volume = 0;
        }
        return volume;
    }

    void setVolume()
    {
        System.out.println("Please input volume setting:");

        Scanner myInput = new Scanner(System.in);
        int v = myInput.nextInt();

        if(power == true)
        {
            if(v<=10 && v>= 0)
                {
                    volume = v;
                }
            else
                {
                    volume = 1;
                }
        }
    }

    boolean getPower()
    {
        return power;
    }

    void setPower()
    {
        if(power == true)
        {
            power = false;
        }
        if(power == false)
        {
            power = true;
        }
    }
}
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.