Reputation: 35
I have just started learning Java and I reached till arrays , I was preparing this program(From a book) on replacing space ' ' with '.' (dots) and i am not able to understand this specific line (its not mentioned even in the book I am learning from).
Please help me out.
class SpaceRemover{
public static void main(String[] args){
String mostFamous = "Hey there stackoverFLow ";
char [] mf1 = mostFamous.toCharArray();
for(int dex = 0; dex<mf1.length;dex++)
{
char current = mf1[dex]; // What is happening in this line ??
if (current != ' ') {
System.out.print(current);
}
else{
System.out.print('.');
}
}
System.out.println();
}
}
Someone please explain what is happening in "char current = mf1[dex];"
Thanks a lot for your time.
Upvotes: 1
Views: 509
Reputation: 46
Basically a String in java is an array of characters. So what the above code does is converts the string to an array of chars so that it can access each index of the array later on. Then the code enters into a for loop in order to iterate through all the indecies of the char array.
Assuming that that is already clear to you, the code now creates a char variable which holds the current index of the array.
char current = mf1[dex];
mf1 is your char array that represents the string. dex is the current index of the char that is determined by the for loop. So by doing this we can check each character (letter) of the char array. Now if the char "current" is a blank space we can replace it with a dot.
Upvotes: 1
Reputation: 44
After this statement, char [] mf1 = mostFamous.toCharArray();
mf1[0]=H, mf1[1]=e, mf1[1]=y...
so at this line, char current = mf1[dex];
so, in first iteration, current=H
, second iteration current=e...
Upvotes: 0
Reputation: 49
char current = mf1[dex];
this will return the char element in char array whose index is dex
This is quite a basic usage of array.
Good luck with your study.
Upvotes: 0
Reputation: 425238
It assigns the element of the char
array mf1
at the index dex
to the char
variable current
.
Note that the for loop and that line may be simplified by using the foreach
syntax; these two code blocks are equivalent:
// Your code
for(int dex = 0; dex<mf1.length;dex++) {
char current = mf1[dex];
// Equivalent code
for (char current : mf1) {
But further, the whole method may be replaced by one line:
public static void main(String[] args){
System.out.println("Hey there stackoverFLow ".replace(" ", "."));
}
Upvotes: 0
Reputation: 155
Here is solution
class SpaceRemover{
public static void main(String[] args){
String mostFamous = "Hey there stackoverFLow ";
char [] mf1 = mostFamous.toCharArray();
// mf1 contains={'H', 'e','y',' ','t','h',.........}
for(char current: mf1)
{
//the for-each loop assigns the value of mf1 variable to the current variable
//At first time the 'H' is assigned to the current and so-on
System.out.print(current==' '?'.':current );
}
System.out.println();
}
}
}
Upvotes: 0
Reputation: 178
The line
char current = mf1[dex];
is placed inside a for loop where the variable dex is incremented each time the loop is iterated. The variable dex
is the zero-based index of the array. On the left hand side of the assignment operator (=), you are declaring a variable named current
of type char
. On the right hand side of the assignment operator you are accessing the dex-th character of your CharArray, if you start counting from zero. The assignment operator binds the variable you declared with the value of the character you specified on the right hand side.
For example, the first time the loop is run, dex
would start at 0, hence mf1[dex]
(or mf1[0]
) is just 'H'.
Upvotes: 0
Reputation: 73
char current = mf1[dex];
This line gets values from the mf1
char array and assign to the current
variable according to the dex
, dex
works as index to the array element and it increments with the running loop.
Upvotes: 0
Reputation: 304
You are getting the dex
th character/item within the character array mf1
(hence mf1[dex]
) and storing it into the local variable current
.
Upvotes: 2
Reputation: 54248
The for-loop is iterating the string mostFamous
character by character.
the line you are asking is to get the character at specific position. Function is similar to JavaScript's charAt(i)
Upvotes: 0
Reputation: 1675
It's getting the character at index idx
in the array mf1
and storing its value in the current
variable.
Upvotes: 0