• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with toString and boolean operator

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.






 
Ranch Hand
Posts: 30
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 65
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it says arrayindexoutofbounds-1. that was the error i got.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic