• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

whats wrong with my code ?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ref is always 0. You are incrementing y but using ref for accessing the array.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why do yopu need ref? You can use y itself for the index.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem you have is that you are always .... Try this.

 
Marshal
Posts: 80749
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of ref-- you can get the value from the index array using the "y" as the index (not index array) value.
 
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Campbell Ritchie
Marshal
Posts: 80749
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You sure it won't throw a CannotFindThatExceptionException?
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 80749
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic