Could u tell me why the following problem is coming on running the program where im getting the out twice each time wen i shd get it only once , wen im entering a input im getting the menu twice

import java.io.*;


class Television{


        private String MANUFACTURER;
        private int SCREEN_SIZE;
        private boolean powerON;
        private int volume = 0;
        private int channel = 0;


        public Television(String brand, int size){
                MANUFACTURER = brand;
                SCREEN_SIZE = size;
        }

        public void setChannel(char station){
                if(station == 'g') channel++;
                if(station == 'h') channel--;
        }

        public void power(){
                if( powerON == false) powerON = true;
                else powerON = false;
        }

        public void increaseVolume(){
                volume++;
        }

        public void decreaseVolume(){
                volume--;
        }
        
        public int getChannel(){
                if(channel == 100){channel = 0;}
                if(channel < 0 ){channel = 99;}
                return channel;
        }

        public int getVolume(){
                if(volume < 0) volume = 0;
                return volume;
        }

        public String getManufacturer(){
                return MANUFACTURER;
        }

        public int getScreenSize(){
                return SCREEN_SIZE;
        }

        public static void main(String args[] )throws Exception{

                BufferedReader br = new  BufferedReader(newInputStreamReader(System.in));
                boolean q1 = true;

                Television t1 = new Television("Sony Television",32);
                Television t2 = new Television("ONIDA Television",29);

                while(q1){

                        System.out.print("You have a  " + t1.getScreenSize()  +" 
inch  " + t1.getManufacturer() + "  in");
                        if(t1.powerON == false) System.out.println("  off mode");
                        else System.out.println("  on mode");
                        System.out.println("1: Press p to toggle power state.");
                        System.out.println("2: Press g and h to increase or decrease
channel.");
                        System.out.println("3: Press f and j to increase or decrease
volume.");
                        System.out.println("4: Press q to stop performing
operations.");

                        System.out.println("Volume: " + t1.getVolume());
                        System.out.println("Channel: " + t1.getChannel());
                        char a = (char)br.read();
                         if(a == 'p'){
                        System.out.println("1");
                        t1.power();
                        System.out.println("2");
                        }
                        if(a == 'g') t1.setChannel(a);
                        if(a == 'h') t1.setChannel(a);
                        if(a == 'f') t1.increaseVolume();
                        if(a == 'j') t1.decreaseVolume();
                        if(a == 'q') q1 = false;

                }
        }

}

Recommended Answers

All 31 Replies

I didn't read it very well, but I have a feeling it's that loop not being set, or if it is, it's being reset back to it's previous value.

Again, I didn't read it well enough to know for sure, but my best guess, would be that loop doing whatever it's doing twice.

Hope this help,
JT

Look at your code. What conditions will cause the menu to be printed twice?
Add a println to show what the user entered and was read into the a variable.

Run your program and copy the console and paste it here so we can see what you entered and what the program did.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Look at your code. What conditions will cause the menu to be printed twice?
Add a println to show what the user entered and was read into the a variable.

Run your program and copy the console and paste it here so we can see what you entered and what the program did.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

The following is the output i got on command prompt:
C:\Users\dell\java>javac Television.java

C:\Users\dell\java>java Television
You have a 32 inch Sony Television in off mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 0
Channel: 0
p My input
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 0
Channel: 0
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 0
Channel: 0
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 0
Channel: 0
g My input
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 0
Channel: 1
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 0
Channel: 1
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 0
Channel: 1
f My input
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 1
Channel: 1
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 1
Channel: 1
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 1
Channel: 1

Can you explain where the output from the program is incorrect?
You show your input in RED.
What is supposed to happen after you enter your input?

Have you looked at the code in the program to see what it is coded to do in the loop after a user enters a letter?

To help you see what is happening, Print out the variable: a after it is read in to show how the loop is repeating.

Can you explain where the output from the program is incorrect?
You show your input in RED.
What is supposed to happen after you enter your input?

Have you looked at the code in the program to see what it is coded to do in the loop after a user enters a letter?

To help you see what is happening, Print out the variable: a after it is read in to show how the loop is repeating.

well i have checked a lot of times and even checked it on a different operating system :P in Linux this program has the 6 println statements repeating twice but it shd be happening only once
and in command prompt on WINDOWS 7 its repeating 3 times for a single input .....

ive checked my code and here is the while loop

while(q1 == true){

                        System.out.print("You have a  " + t1.getScreenSize()  +" inch  " + t1.getManufacturer() + "  in");
                        if(t1.powerON == false) System.out.println("  off mode");
                        else System.out.println("  on mode");
	               
    // THE FOLLOWING STATEMENTS ARE BEING REPEATED 2 TIMES IN LINUX FOR ONE USER INPUT ND 3 IN CMD OF windows 7
                       
                        System.out.println("1: Press p to toggle power state.");
                        System.out.println("2: Press g and h to increase or decrease channel.");
                        System.out.println("3: Press f and j to increase or decreasevolume.");
                        System.out.println("4: Press q to stop performing   operations.");
                        System.out.println("Volume: " + t1.getVolume());
                        System.out.println("Channel: " + t1.getChannel());
                       
	                char a = (char)br.read(); // input from user being read
                         
	                if(a == 'p') t1.power();
                       
                        
                        if(a == 'g') t1.setChannel(a);
                        if(a == 'h') t1.setChannel(a);
                        if(a == 'f') t1.increaseVolume();
                        if(a == 'j') t1.decreaseVolume();
                        if(a == 'q') q1 = false;
	      
	}

To help you see what is happening, Print out the variable: a after it is read in to show why the loop is repeating.

Also change your code to this to see what is happening:

else if(a == 'g') t1.setChannel(a);
                        else if(a == 'h') t1.setChannel(a);
                        else if(a == 'f') t1.increaseVolume();
                        else if(a == 'j') t1.decreaseVolume();
                        else if(a == 'q') q1 = false;
                        else {
                          System.out.println("unknown choice=" + a + "<");
                        }

To help you see what is happening, Print out the variable: a after it is read in to show why the loop is repeating.

Also change your code to this to see what is happening:

else if(a == 'g') t1.setChannel(a);
                        else if(a == 'h') t1.setChannel(a);
                        else if(a == 'f') t1.increaseVolume();
                        else if(a == 'j') t1.decreaseVolume();
                        else if(a == 'q') q1 = false;
                        else {
                          System.out.println("unknown choice=" + a + "<");
                        }

no the same problem is there ..... the while loop is being repeated 3 times without waiting for the input

What value was printed out when you printed the contents of a?

Did you change your list of if statements as I suggested 2 posts back?

What value was printed out when you printed the contents of a?

Did you change your list of if statements as I suggested 2 posts back?

ya i did change the program as u said but theres no change ....

the while loop is repeating the 6 println statements 2 more times without waiting for me to give the input.

in othr words 6 statements(the println ones) were repeated twice then i was able to enter a new input ...

there is only 1 call to read() in the while loop .... so it should execute only once ... but instead its taking input only once and then bypassing call to read() the next two times

Well, I can't help you right now but I saw the problem. The easiest way to figure it out and fix it is to debug it. By the way, why did you create t2 again? You didn't use it so I don't see it's utility anywhere.

So, debug it in order to understand what happened

What value was printed out when you printed the contents of a?

Did you change your list of if statements as I suggested 2 posts back?

but theres no change ..

If you did what I suggested you should get some printout from those printlns.
What do you see printed out from the new printlns you added?

Well, I can't help you right now but I saw the problem. The easiest way to figure it out and fix it is to debug it. By the way, why did you create t2 again? You didn't use it so I don't see it's utility anywhere.

So, debug it in order to understand what happened

ill use t2 later ... acutually this was the first part of the program.

debugging i m doing but i have no idea watsoever y the problem is occuring ...
in fact one of my frends also had a similar output....

could this be a problem with the JVM?

What value was printed out when you printed the contents of a?

If you did what I suggested you should get some printout from those printlns.
What do you see printed out from the new printlns you added?

same as before ...

here is the prntout :

C:\Users\dell\java>javac Television.java

C:\Users\dell\java>java Television
You have a 32 inch Sony Television in off mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 0
Channel: 0
p
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 0
Channel: 0
<nknown choice=
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 0
Channel: 0
unknown choice=
<
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 0
Channel: 0
f
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 1
Channel: 0
<nknown choice=
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 1
Channel: 0
unknown choice=
<
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 1
Channel: 0
m
unknown choice=m<
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 1
Channel: 0
<nknown choice=
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 1
Channel: 0
unknown choice=
<
You have a 32 inch Sony Television in on mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decreasevolume.
4: Press q to stop performing operations.
Volume: 1
Channel: 0

Do you see that when you are reading 3 characters from System.in?
The character plus the lineend and the carriage return.

Okay. Well, look at your loop, your if statements. Try to put print statements between your lines (That's what my instructors always tell me). If you don't find anything then, I'm gonna try to see if I can find it for you but it will be by Wednesday. I have to study for 2 exams tomorrow. Good luck!

Okay. Well, look at your loop, your if statements. Try to put print statements between your lines (That's what my instructors always tell me). If you don't find anything then, I'm gonna try to see if I can find it for you but it will be by Wednesday. I have to study for 2 exams tomorrow. Good luck!

Thanks !! :)

All the best for your exams :)

Do you see that when you are reading 3 characters from System.in?
The character plus the lineend and the carriage return.

yeah if i put in put 'g''g''f' that is i typed "gff" as in put

then the read() takes one char ata tym .... i should get 3 println statements
but the last of these 3 gets repeated 2 more times
and i end up with 5 statements

Here is what I get when I enter: ggf
I used this to print out the value of a:
System.out.println("a=" + (int)a + "<"); //<<<<<<


D:\JavaDevelopment\Testing\ForumQuestions6>java Television
You have a 32 inch Sony Television in off mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decrease volume.
4: Press q to stop performing operations.
Volume: 0
Channel: 0
ggf <<<<<<<<<<<<<< ggf<ENTER> typed on the console
a=103< <<<<< the g
You have a 32 inch Sony Television in off mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decrease volume.
4: Press q to stop performing operations.
Volume: 0
Channel: 1
a=103< <<<<<<<<< The second g
You have a 32 inch Sony Television in off mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decrease volume.
4: Press q to stop performing operations.
Volume: 0
Channel: 2
a=102< <<<<<<<<<< The f
You have a 32 inch Sony Television in off mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decrease volume.
4: Press q to stop performing operations.
Volume: 1
Channel: 2
a=13< <<<<<<< the carriage return
<nknown choice=
You have a 32 inch Sony Television in off mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decrease volume.
4: Press q to stop performing operations.
Volume: 1
Channel: 2
a=10< <<<<<<<<<<< the line end
unknown choice=
<
You have a 32 inch Sony Television in off mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decrease volume.
4: Press q to stop performing operations.
Volume: 1
Channel: 2

Here is what I get when I enter: ggf
I used this to print out the value of a:
System.out.println("a=" + (int)a + "<"); //<<<<<<


D:\JavaDevelopment\Testing\ForumQuestions6>java Television
You have a 32 inch Sony Television in off mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decrease volume.
4: Press q to stop performing operations.
Volume: 0
Channel: 0
ggf <<<<<<<<<<<<<< ggf<ENTER> typed on the console
a=103< <<<<< the g
You have a 32 inch Sony Television in off mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decrease volume.
4: Press q to stop performing operations.
Volume: 0
Channel: 1
a=103< <<<<<<<<< The second g
You have a 32 inch Sony Television in off mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decrease volume.
4: Press q to stop performing operations.
Volume: 0
Channel: 2
a=102< <<<<<<<<<< The f
You have a 32 inch Sony Television in off mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decrease volume.
4: Press q to stop performing operations.
Volume: 1
Channel: 2
a=13< <<<<<<< the carriage return
<nknown choice=
You have a 32 inch Sony Television in off mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decrease volume.
4: Press q to stop performing operations.
Volume: 1
Channel: 2
a=10< <<<<<<<<<<< the line end
unknown choice=
<
You have a 32 inch Sony Television in off mode
1: Press p to toggle power state.
2: Press g and h to increase or decrease channel.
3: Press f and j to increase or decrease volume.
4: Press q to stop performing operations.
Volume: 1
Channel: 2

could you please explain me the reason for the carriage return?? y is a still bieng assigned a value even though there is no input from my side ???? a = 13 followed by a = 10 , why are these values coming ???
not only that for any normal input "p" again a = 13 folowed by a = 10 step is occuring .....
wat could be the reason for this ? and can this problem be removed .....

The windows OS sends those values: (10 & 15) to the System.in class when you press Enter.

To get rid of this problem, you should use a class that will not send those characters when you read from the console. The Scanner class is one choice.

You probably want to read lines, not characters.

When you press "p" and then Enter, your program will receive the "p" character and a newline sequence: On Windows, you will receive three characters -- 'p', '\r' and '\n'. On Linux/Unix, you will receive two characters -- 'p' and '\n'. (And on Apple Macs, should you run it there, you'll get two -- 'p' and '\r'.)

You probably want to read lines, not characters.

When you press "p" and then Enter, your program will receive the "p" character and a newline sequence: On Windows, you will receive three characters -- 'p', '\r' and '\n'. On Linux/Unix, you will receive two characters -- 'p' and '\n'. (And on Apple Macs, should you run it there, you'll get two -- 'p' and '\r'.)

thank you for the informaton :)

The windows OS sends those values: (10 & 15) to the System.in class when you press Enter.

To get rid of this problem, you should use a class that will not send those characters when you read from the console. The Scanner class is one choice.

Thanks :) the program is now working ....
and i also gained some experience with the read() method :P i mean seriously i never thought that sometimes the input would depend on the os

Sorry to take so long, I wanted you to see what was happening.
The OS can have an effect on your program. Java tries to remove most of the OS dependencies but there are a few left.

Sorry to take so long, I wanted you to see what was happening.
The OS can have an effect on your program. Java tries to remove most of the OS dependencies but there are a few left.

Would u please tell me any more such os dependencies ? then i can be more careful from the future :)

Sorry, I don't have a list.

Sorry, I don't have a list.

ok no problem....
thanks for ur help :D

File.separator is an extremely common issue: In Windows it's "\tmp\filename.ext". In Unix/Linux/Mac it's "/tmp/filename.ext".

See also File.pathSeparator for CLASSPATH.

Java makes it possible to write code that will run on all platforms. But it can't hide all the differences between platforms. Sometimes your code must be aware of the differences, and take appropriate actions.

Have you found any solution for your problem?

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.