This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes whats wrong with my code ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "whats wrong with my code ?" Watch "whats wrong with my code ?" New topic
Author

whats wrong with my code ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

i have string array of size 3 and index 0,1,2, contains literals which are " you "
" is " " hot " and then i have created one more array of type int which is index , whose job is to shuffle the orignal string index .to achieve this , in front of index[0] i have made 2 , and in front of index[1] i have made 1 and so that it could point to the names[1] . that means now i want to display reverse which should be " hot is you " on console but the program is just printing " hot ! hot ! hot !" instead of hot ! is you
can any one help me fixing and understanding why i m getting this problem ?
big ups for those who tried.


output is

C:\Users\Admin\Documents\java source code\Niit\testdrive>javac ArrayTest.java

C:\Users\Admin\Documents\java source code\Niit\testdrive>java ArrayTest
hot ! hot ! hot !
C:\Users\Admin\Documents\java source code\Niit\testdrive>


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

ref is always 0. You are incrementing y but using ref for accessing the array.


Mohamed Sanaulla | My Blog
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

Mohamed Sanaulla wrote:ref is always 0. You are incrementing y but using ref for accessing the array.

so you mean i should increment ref after y++.
is that you mean ?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

why do yopu need ref? You can use y itself for the index.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688



Mohamed Sanaulla wrote:ref is always 0. You are incrementing y but using ref for accessing the array.


tried this , now it is printing hot! once but no other indexes like "is" and "u"
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

You are complicating the code.
Also you need to check the values of ref before the start of the loop. Please analyze the code once again and this time use a pen and paper to write down different values of the variables at different point in code.
Executing the program on a machine is not the only way to solve the given problem. Often using paper and pen would give you more understanding than just execution of the code
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

Mohamed Sanaulla wrote:You are complicating the code.
Also you need to check the values of ref before the start of the loop. Please analyze the code once again and this time use a pen and paper to write down different values of the variables at different point in code.
Executing the program on a machine is not the only way to solve the given problem. Often using paper and pen would give you more understanding than just execution of the code

ok thanks for the reply , but can any one out there will modify my code to get the desired output .
it will be appreciable . according to the index array .
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Why can't you do it? Did you follow what I told? I can point out the error but what would you learn from that?
Eder Suarez
Greenhorn

Joined: Jun 15, 2011
Posts: 13
The problem you have is that you are always .... Try this.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
Please have a look at what it says at the top of the "Beginning Java™" forum:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Mohammed Sanaulla was right saying
. . . I can point out the error but what would you learn from that?
It does no good simply to give such an answer; don't be annoyed with me, but I have removed your answer so as not to deprive naved momin of the chance to learn.

Jesper, Maneesh, Rob, please don't be annoyed with me for pulling rank.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

Mohamed Sanaulla wrote:Why can't you do it? Did you follow what I told? I can point out the error but what would you learn from that?


i have fixed it ,

but here is another problem when i changed index values , the value of ref is also getting changed . for eg :
if i made index values as in reverse orders like 2,1,0 the value of ref is 2 and when i changed value of index to somewhat like this 1,2,0 then the value of ref is changed to 1 before entering into while loop , only part i didnt understand is why the hell is ref changing its value when i changed index values ?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

With what value are you initializing the ref?

Unrelated- what if there more than three strings? Or you are not sure about the length of the string array?
Hint: find out the length of the string array. Now from the value of length keep decrementing until its greater than Zero and use this as an index for the string array.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

Mohamed Sanaulla wrote:Why can't you do it? Did you follow what I told? I can point out the error but what would you learn from that?

i have understand why the ref is changing its value , ref is always that value which i give to index[0] , but still when i changed some index value like consider the below code , mainly index[] part which gives the same output as earlier even though the desired output is " hot ! you is ". the output available is " hot ! is you "
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Instead of ref-- you can get the value from the index array using the "y" as the index (not index array) value.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Campbell Ritchie wrote:Jesper, Maneesh, Rob, please don't be annoyed with me for pulling rank.

No probs. But you do know that that code will fail to compile because it can't find the exception class, right?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
You sure it won't throw a CannotFindThatExceptionException?
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

Mohamed Sanaulla wrote:Instead of ref-- you can get the value from the index array using the "y" as the index (not index array) value.

i didnt get you ,
my question is because of this line my ref value is 2 but i had made it to 0 before entering into the while loop , so after entering the while loop , in 1st iteration the statement should go to which should call to and "hot ! " should get printed on the console and likewise every values of should be printed but it is not happening this way , where i m getting wrong please elaborate yourself so that i can understand . thanks for replying

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
You are going to have to write down the values of ref and which element of the names array you are accessing. Something like this
Line 3: first appearance of ref. name[ref] = "Campbell"
Line 4: ref is now 4
Line 5: uses name[ref] = "Ritchie"

That sort of thing. Get a sheet of paper, a pencil and a large eraser, and then you can understand what is happening.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

You want 'ref' to take values from the array 'index'. Which means it should start with index[0] and should go on till index[y]. Now, look into the (original) code (from your first post) and find out how many times 'ref' is actually assigned a value. What should you do to make sure 'ref' is assigned a different value from 'index' array, per loop iteration?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: whats wrong with my code ?
 
Similar Threads
whats wrong with my code ?
Be the compiler excercise in chap 3
error at runtime trying static initializer first time please help
Trying to learn enumerated types of class
what is the problem with my java code