File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes randomly assigning the wrong values into a variable in switch case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "randomly assigning the wrong values into a variable in switch case" Watch "randomly assigning the wrong values into a variable in switch case" New topic
Author

randomly assigning the wrong values into a variable in switch case

aresh babu
Ranch Hand

Joined: Aug 31, 2008
Posts: 65
Hi

I have a code like this



My problem here it is.,
when the accessMethod value is 0 - interMode is assiging as 4050005
when the accessMethod values is 1 - interMode is assigning as 4050002

This issues is happening randomly but not always..
I am using java version java 1.5.0

Could you please let us know how to fix this issue..

Thanks & Regards
Aresh
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

In the future, when posting code, please UseCodeTags(←click) and use proper indentation. I've fixed it for you this time. See how much more readable it is?

As for your problem:


when the accessMethod value is 0 - interMode is assiging as 4050005
when the accessMethod values is 1 - interMode is assigning as 4050002


No, it's not. I 100% guarantee you it's not. Not with that code. Either you're running different code (maybe old code with reversed logic, and you didn't recompile), or else you're misinterpreting what you're seeing. Java code doesn't just behave randomly, and if something that simple and fundamental were broken, people would be screaming up and down about it and nothing would work.

You need to check your assumptions more carefully, and if you can't figure it out, you'll have to post more details here about what you're actually doing and how you're observing the code's behavior so that somebody here can point you in the right direction.

If you're observing the problem intermittently, it is almost certainly a multithreading problem. You're probably not using proper synchronization somewhere.

Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 908
    
    5
I once worked in a programming environment where parameters were passed by reference, and constants were not kept track off.
That is, you were able to pass a literal constant value to a function which then changed it!
From that moment on, all references to that literal constant were referring to a different value: they got broken.

Boy, those were the days!
Sumit Suresh Rao
Greenhorn

Joined: Feb 16, 2009
Posts: 13
As Jeff Verdegan said
Java code doesn't just behave randomly


Probably you are doing some basic mistake. Take a deeper look at your code. The data that you have provided is insufficient (i.e. what is accessMethod etc.) sharing your entire class with us might help.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

If you REALLY think your code is doing that, prove it to yourself. System.out.println can be your best friend. When things are happening that just don't make any sense, I will stick dozens of S.o.p statements in. print the value of accessMethod just before your switch statement. inside each case, print "I am in case 0" (or whichever case you are really in). Print out a line like "I am assigning the value of 4050002 to intermode" just before you do the assignment, then print "i just assigned the value of 4050002 to intermode just after you assign it.

just after you close your switch statement, print the value of intermode again, before ANY other line of code runs.

My guess is that either a) accessMethod isn't what you think it is, b) you are missing break statements somewhere, or c) intermode is being set to something in another spot.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
wood burning stoves
 
subject: randomly assigning the wrong values into a variable in switch case