My program does not work properly
Author |
Message |
mix12
|
Posted: Thu Nov 25, 2004 5:32 pm Post subject: My program does not work properly |
|
|
My return method is not wokring properly
I have to do a school assignment.
There are 3 inputs, the first one is the main string, the second input is the substring and the 3rd input is the prefix
I have to make it so that every occurance of the substring within the main string has to have the prefix infront of it.
(Just if you are wondering, i am using the the HSA console)
This is the return method:
private static String prefix (String sentence, String word, String before)
{
int y = 0;
int x = 0;
String insert = "";
String mid = "";
String end = "";
while (true)
{
if (sentence.charAt (y) == word.charAt (x))
{
while (true)
{
if (word.charAt (x) != sentence.charAt (y))
{
break;
}
if (x == (word.length () - 1))
{
while (true)
{
int z = 0;
insert += sentence.charAt (z);
z++;
if (z == y)
{
break;
}
}
mid = before;
while (true)
{
int a = y;
end += sentence.charAt (a);
a++;
if (a == sentence.length () - 1)
{
break;
}
}
}
x++;
y++;
}
}
x = 0;
y++;
if (y == sentence.length () - 1)
{
break;
}
}
if (insert.length () < 1)
{
return sentence;
}
else
{
return (insert + mid + end);
}
} |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
|
|
|
|
Hikaru79
|
Posted: Thu Nov 25, 2004 9:16 pm Post subject: (No subject) |
|
|
So would using indexOf. If you have the Holt Java textbook (which you should, if you're using HSA), turn to page 180 and go from there =) Good luck.
Oh, and use code tags. it makes:
System.out.println ("Use code tags!");
look like:
code: |
System.out.println ("Use code tags!");
|
|
|
|
|
|
|
zylum
|
Posted: Thu Nov 25, 2004 10:53 pm Post subject: (No subject) |
|
|
if i understood the question correctly, i beleive that's a one liner:
return sentence.replaceAll(word, before+word); |
|
|
|
|
|
wtd
|
Posted: Thu Nov 25, 2004 11:07 pm Post subject: (No subject) |
|
|
zylum wrote: if i understood the question correctly, i beleive that's a one liner:
return sentence.replaceAll(word, before+word);
I think that for the purpose of the exercise, he has to write the logic himself. |
|
|
|
|
|
mix12
|
Posted: Sat Nov 27, 2004 9:11 am Post subject: Thank you, but.... |
|
|
return sentence.replaceAll works, but...
I am only allowed to use two string built-in methods -charAt() and length() |
|
|
|
|
|
mix12
|
Posted: Sat Nov 27, 2004 9:31 am Post subject: I got it to work a bit more, just some left |
|
|
It is almost finished but i still have to find the substring within the mainstring as many times as it occurs.
I can find it once and put a perfix infront of it.
But i can to find every occurance and out a prefix infront.
This is the new return method:
private static String prefix (String sentence, String word, String before)
{
String insert = "";
int y = 0;
int x = 0;
int z = 0;
while (y != sentence.length () - 1)
{
while (insert.length () <= sentence.length ())//problem is here
{
if (word.charAt (x) != sentence.charAt (y))
{
break;
}
if (x == word.length () - 1)
{
int first = y - (word.length () - 1);
while (z != first)
{
insert = insert + sentence.charAt (z);
z++;
}
insert += before;
int a = first;
while (a != sentence.length ())
{
insert += sentence.charAt (a);
a++;
}
}
x++;
y++;
}
x = 0;
y++;
}
if (insert.length () < 1)
return sentence;
return (insert);
} |
|
|
|
|
|
|
|