• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with toString and boolean operator  RSS feed

 
atish shrestha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this compiles but i am not getting right output.......this is the output that i was hoping but somehow it is not getting right......can someone help me what is the problem with my code.






 
Stefano Carniel
Greenhorn
Posts: 27
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you to use code tags otherwise your code is unreadable. Then post the output you got and the one you want
 
Myyron Murray Latorilla
Ranch Hand
Posts: 65
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
atish shrestha wrote:this is the output that i was hoping but somehow it is not getting right


There is no output on your post.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Stefano Carniel wrote:I suggest you to use code tags otherwise your code is unreadable. Then post the output you got and the one you want
Since you are new, I have added the code tags, but I had to shorten the long lines and you can see how it is done. Doesn't the post look better now .

Agree: you must tell us what actually goes wrong, otherwise we cannot help.
I can see some things wrong with your code. Never use == true or == false. Not only is that poor style, but also writing = by mistake instead of == causes serious errors. Write if (b) ... and if (!b) ...
Or while (b) ... and while (!b) ...
Minor error: don't use \n. I see you know about String#format: always use %n rather than \n.
You have logic discrepancies between the setVolume and increaseVolume methods. One of those methods will happily set the volume to 11. Similarly another method can set it negative.
Why have you got the multiple ifs in the channel name method, when you have already set the names? Why is the channel name array not marked private? Returning that array breaches encapsulation, but I shall leave others to work out how.
 
atish shrestha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply and also for helping me to align the program. This is my first post so i did not know how to use code tag. I did not know we could use %n for the new line. Thank you for the suggestion. we were taught to use \n. so i was using \n for new line. Is there anything wrong with array?


The output should look like this
TV State: On
Channel No: 4
Channel Name: PBS
Volume: 6


but my output look like this

Tv State:
channel No: 0
Channel Name: [Ljava.lang.String;@241455d75
volume: 0
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in this method public String[] getChannelName()
Is should not return String[]. It should return String.
It should be a very simple method that returns one element from the array. Instead it modifies the array and returns it.
Those ifs are unnecessary.
This method should:
- "translate" channel number to an array index
- validate array index (this is optional, if you don't do that and an input is wrong an exception will be thrown)
- return array element from the index.
Very simple.

As for your output:
Tv State is not present because you didn't put it into toString method.
Channel no and volume are set to 0: check ifs in setVolume and setChannel methods.
Channel name: this is result of String[].toString method. You try to display an array of channels while you should display only one channel.
 
atish shrestha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i modified

public String getChannelName()
{

return channelName[channel];
}

but still the output is not right

TV State:
channel no: 0
channel Name: CBS
volume: 0


T2 = TV State:
channel no: 0
channel Name: CBS
volume: 0


 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read my previous post. I edited it so you might have not seen what I wrote about your output.

BTW, are you sure you want to return channelName[channel]?
Your chanells are 1-based but arrays in Java are 0-based.

So channel no. 1 has an index 0.
Channel no. 2 has index 1 etc.
I think you see a pattern now ;).
 
atish shrestha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do return boolean operator in toString method.

i want to display as TV state: On or OFF.....Any suggestion please?

i was able to display channel no and channel name but the problem is since array starts from 0 index. so it is displaying wrong channel name. i know we should subtract -1 .



this helped me to display channel number and name. but

if i write

return channelName [channel-1];

it gives me error. where do you suggest me to decrease the channel by 1 so that it display right channel Name.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags.

First, you may put a String into your String.format in toString() in place of your state On/Off.
The value of the String will depend on this.power.

As for subtracting 0 giving you an error... What error? Can you put a trace here?


EDIT:
you get an error because you use setChannel and setVolume methods inside your constructor. You should not do that. Just initiate your variables directly.
Those method will have no effect on your object because inside your constructor, power is false!
So your channel is 0. So you try to access index -1 which is illegal.
 
atish shrestha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it says arrayindexoutofbounds-1. that was the error i got.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
atish shrestha wrote:it says arrayindexoutofbounds-1. that was the error i got.

See my previous post. I edited it.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:you get an error because you use setChannel and setVolume methods inside your constructor. You should not do that. Just initiate your variables directly.

And remember to check if the input is valid. Maybe a private method isChannelNumberValid?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!