• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

what is bug in this code, the output of this code gives 0's

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code is rather cryptic to read. What did you expect the output to be?
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I put your code between [code] and [/code] ubb code blocks - you should use these so that your code is easy to read.

I cannot see any bug in that program - it looks like it comes from a tutorial or a mock exam on variable scope, and it looks like it is designed to print zeroes as part of it's demonstration.

As Ulf suggested - maybe telling us what you think the output should be would help - we can then start discussing why this is not the case.

Could you please provide attribution for the source code? It is a requirement of this site so that we know that something has not been illegally copied.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here you are not assigning the values in your input method, you are creating two more local method variable which are having method scope.

You are not assigning your instance variables any where in your code, int instance variables by default will be assigned to zeros ,that is what printing in your output.

To make it work:

void input()
{
d=10; // remove int keyword
e=54; //remove int keyword
}
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the instance variables gets HIDDEN by local variables that you initialized in method. rest is explained by Ashok.
 
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remove int from method input(), it treats it as local variables of input method.
 
Ulf Dittmer
Rancher
Posts: 43028
76
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're all jumping the gun. This is clearly not useful production code, so it's entirely unclear what it is supposed to be doing. krishna hasn't even said why he thinks there's a bug in the code.
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic