ok heres my code

import java.util.Scanner;

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

        tv.printImage();

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

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

        do
        {
            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("");

            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
            {
            System.out.println("");
            System.out.println("Error - Input not recognised!");
            System.out.println("");
            }
        }while(!(myInput.equals(s4)));
    }
}

when i run this code the loop just continues to move by itself without allowing me to make an input how can i change this so that it loops back to when i first make an imput until i input string s4 (Q)

Recommended Answers

All 6 Replies

Move the code that reads the input in the while loop

commented: looks like you've got a lot of patience +3

now the

}while(!(my Input.equals(s4)));

does not work because the myInput variable is not stated outside of the loop

now the

}while(!(my Input.equals(s4)));

does not work because the myInput variable is not stated outside of the loop

You're right my Input is not, but myInput is.

Unfortunatly "myInput" is a scanner, not a String, and you want to compare Strings, so aside from moving the "read" line into the while loop (but leaving the declaration of Input1 outside of the while loop), don't you think you should be using Input1 in that conditional expression?

ok ihave changed myInput in the while statement to input1

im assuming by the "read line" you mean one of the parts of the scanner

i cannot have both parts of the scanner both in and out of the loop as it then complains that i have already defined myinput and input1 out of the loop

when both parts of the scanner are just in the loop it complains that Input1 is not defined

when both parts of the scanner are outside of the loop the initail problem occurs (see my first post)

and i cannot seperate the 2 parts of the scanner as without the

String Input1 = myInput.next();}

being in the loop the myInput variable is not defined

and without

Scanner myInput = new Scanner(System.in);

being in the loop the input1 in the while statement is not defined

Scanner myInput = new Scanner(System.in);
String input1 = "";
...
do {
  input1 = myInput.next();
  ...
} while (....

Split the declaration from the definition and put the definition as the first line of the loop.

commented: this post helped my fix my problem and was very helpful +1

that fixes my porblem thnx alot you two

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.