wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes why does this code work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why does this code work" Watch "why does this code work" New topic
Author

why does this code work

Dmitri Makovetskiy
Ranch Hand

Joined: Jun 21, 2010
Posts: 128



This could should obtain a few words and translate them from one language to another. i got this code from my teacher and i dont understand a few things





Here is the second part of the code:

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14157
    
  19

First part:

Line 39: I don't understand what you're asking there. 'word' is filled by concatenating letters in the loop on lines 34 - 38.

Line 44: If both i and count are 0, then the loop will stop at the beginning (it won't run at all). However, most of the time, count will not be 0. Note that count is incremented in line 20.

Second part:

Line 9: The loop advances 'index' to the first character in 'sentence' that is not a space (note that line 10 is the body of the loop).

Line 18: The variable 'found' is used to keep track of if the content of 'word' is found in the array 'ger'; lines 19 - 23 loop over 'ger' to see if 'word' is in there, and if so, then 'found' is set to true.

Line 25: 'found' is not a method, it's a variable. It will be true when 'word' was found in the array 'ger', and false if it was not found (see above).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Dmitri Makovetskiy
Ranch Hand

Joined: Jun 21, 2010
Posts: 128
Jesper Young wrote:First part:

Line 39: I don't understand what you're asking there. 'word' is filled by concatenating letters in the loop on lines 34 - 38.

Line 44: If both i and count are 0, then the loop will stop at the beginning (it won't run at all). However, most of the time, count will not be 0. Note that count is incremented in line 20.

Second part:

Line 9: The loop advances 'index' to the first character in 'sentence' that is not a space (note that line 10 is the body of the loop).

Line 18: The variable 'found' is used to keep track of if the content of 'word' is found in the array 'ger'; lines 19 - 23 loop over 'ger' to see if 'word' is in there, and if so, then 'found' is set to true.

Line 25: 'found' is not a method, it's a variable. It will be true when 'word' was found in the array 'ger', and false if it was not found (see above).


what i dont understand, with "word" and "found" variable.. if it is defined in and outside of the looop.. why the variable doesnt die being inside the loop..
i did this code, and when i set my variable word.. i set it like this word="";
and i defined second time inside the loop..
but when i was refering to it, it referred to the variable first defined.. word=""

how does the program selects which variable to refer to , if the same variable is defined twice
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You define the variable 'word' in line 10 - which is the one that is been updated/reassigned in the first part in line 12, 18, 33, 36, 46 and 49 - and in the second part in line 8 and 14.

I can't see where you define the boolean variable 'found' - but it is the same that is been updated/reassigned in line 18 and 22 - and it is been used in line 25.


Regards, Rene Larsen
Dropbox Invite
Dmitri Makovetskiy
Ranch Hand

Joined: Jun 21, 2010
Posts: 128
Rene Larsen wrote:You define the variable 'word' in line 10 - which is the one that is been updated/reassigned in the first part in line 12, 18, 33, 36, 46 and 49 - and in the second part in line 8 and 14.

I can't see where you define the boolean variable 'found' - but it is the same that is been updated/reassigned in line 18 and 22 - and it is been used in line 25.



why wont all those variables inside the loop die and outside the loop dominate ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Because it's the same variable. You're not declaring a new one. It's the same one.
 
wood burning stoves
 
subject: why does this code work