I just need part 3: For problem3(), you keep finding the same letter, because yo
ID: 3905023 • Letter: I
Question
I just need part 3:
For problem3(), you keep finding the same letter, because you are missing something that would cause the subsequent search to look beyond the previous i found.
indexOf works by you supplying the character that you're looking for and the starting location to look for it.
It returns the location where it found the character.
If you want to find all the characters in a given string, you will need to keep incrementing the starting location using a loop.
public class FixMeDomain
{
boolean stop = false;
int count = 100;
String choice = "";
//Do NOT delete any code from the following method. Only add/change.
public void problem1()
{
while(!stop)
{
while(count > 0)
{
System.out.println("Count is: " + count);
}
do
{
System.out.println("Count is " + count);
}
while(count < 50);
stop = true;
}
}
//Do NOT remove any code from problem2(), only change/add code.
public void problem2()
{
for(int i = 0, y = 20; i < 10 && y > 10; i++, y--)
{
System.out.println("Something");
switch(i)
{
case 0:
System.out.println("Case 0");
break;
case 1:
System.out.println("Case 1");
break;
case 2:
System.out.println("Case 2");
break;
case 3:
System.out.println("Case 3");
break;
case 4:
System.out.println("Case 4");
break;
case 5:
System.out.println("Case 5");
break;
case 6:
System.out.println("Case 6");
break;
case 7:
System.out.println("Case 7");
break;
case 8:
System.out.println("Case 8");
break;
case 9:
System.out.println("Case 9");
break;
case 10:
System.out.println("Case 10");
break;
default:
System.out.println("Default");
}
}
}
//Do NOT delete any code. Just change/add.
public void problem3()
{
String word = "supercalifragilisticexpialidocious";
int iCount = 0;
int i = 0;
int index =0;
while (i < word.length() )
{
index = word.indexOf("i", i);
if (index > -1)
{
System.out.println("Got it!");
iCount++;
}
i++;
}
System.out.println("There are this many i's in " + word + ": " + iCount);
}
}
Explanation / Answer
Fixed code for problem 3. Please do rate if it helped.
public class FixMeDomain
{
boolean stop = false;
int count = 100;
String choice = "";
//Do NOT delete any code from the following method. Only add/change.
public void problem1()
{
while(!stop)
{
while(count > 0)
{
System.out.println("Count is: " + count);
}
do
{
System.out.println("Count is " + count);
}
while(count < 50);
stop = true;
}
}
//Do NOT remove any code from problem2(), only change/add code.
public void problem2()
{
for(int i = 0, y = 20; i < 10 && y > 10; i++, y--)
{
System.out.println("Something");
switch(i)
{
case 0:
System.out.println("Case 0");
break;
case 1:
System.out.println("Case 1");
break;
case 2:
System.out.println("Case 2");
break;
case 3:
System.out.println("Case 3");
break;
case 4:
System.out.println("Case 4");
break;
case 5:
System.out.println("Case 5");
break;
case 6:
System.out.println("Case 6");
break;
case 7:
System.out.println("Case 7");
break;
case 8:
System.out.println("Case 8");
break;
case 9:
System.out.println("Case 9");
break;
case 10:
System.out.println("Case 10");
break;
default:
System.out.println("Default");
}
}
}
//Do NOT delete any code. Just change/add.
public void problem3()
{
String word = "supercalifragilisticexpialidocious";
int iCount = 0;
int i = 0;
int index =0;
while (i < word.length() )
{
index = word.indexOf("i", i);
if (index > -1)
{
System.out.println("Got it!");
iCount++;
i = index;
}
i++;
}
System.out.println("There are this many i's in " + word + ": " + iCount);
}
}