-3
regexやreplaceメソッドを使用せずに、 "java"という単語をすべて大文字の "JAVA"以下のコード&をご覧ください。これが最善の方法であるかどうかを確認してください。regexやreplaceメソッドを使用せずに "java"という単語のすべての出現箇所を大文字の "JAVA"に置き換えます。
コード:
import java.util.Scanner;
public class Substitute
{
public static void main(String[] args)
{
String javaString = "java";
String left, right, sub = "", result = "";
int index;
Scanner input = new Scanner(System.in);
System.out.print("Enter a sentence with a word \"java\" and replaces every occurrence of it with \"JAVA\" or type \"quit\" to quit: ");
String sentence = input.nextLine();
while(!(sentence.equalsIgnoreCase("quit")))
{
index = sentence.toLowerCase().indexOf("java");
if(index != -1)
{
if(javaString.equalsIgnoreCase(sentence.substring(index, index + 4)))
{
while(index != -1)
{
left = sentence.substring(index, index + 4);
right = sentence.substring(index + 4);
if(sentence.indexOf(right) <= 0 || sentence.indexOf(right) >= sentence.length())
{
sub = sentence.substring(index, index + 4);
sub = sub.toUpperCase();
if(result == "")
{
result = sentence.substring(0, index) + sub;
}
else
{
result = result.substring(0, index) + sub;
}
index = -1;
}
else if(sentence.indexOf(right) > 0 && sentence.indexOf(right) < sentence.length())
{
sub = sentence.substring(sentence.indexOf(left, index), sentence.indexOf(right, index));
sub = sub.toUpperCase();
if(result == "")
{
result = sentence.substring(0, (sentence.indexOf(left, index))) + sub + sentence.substring(sentence.indexOf(right, index));
}
else
{
result = result.substring(0, (sentence.indexOf(left, index))) + sub + sentence.substring(sentence.indexOf(right, index));
}
index = sentence.toLowerCase().indexOf("java", index + 4);
}
if(index == -1)
{
System.out.println(result);
}
} //end of while loop
System.out.print("Please enter a sentence with a word \"java\" and replaces every occurrence of it with \"JAVA\" or type \"quit\" to quit: ");
sentence = input.nextLine();
index = sentence.toLowerCase().indexOf("java");
result = "";
}
}
else
{
System.out.println("There is no word \"java\" in the sentence.");
System.out.print("Please enter a sentence with a word \"java\" and replaces every occurrence of it with \"JAVA\" or type \"quit\" to quit: ");
sentence = input.nextLine();
index = sentence.toLowerCase().indexOf("java");
result ="";
}
}
}
}
出力:
$ java Substitute
Enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: Program
There is no word "java" in the sentence.
Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: rogram java
rogram JAVA
Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: s java and java
s JAVA and JAVA
Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: ScriptJava
ScriptJAVA
Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: p java
p JAVA
Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: scjava
scJAVA
Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: Javascript
JAVAscript
Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: Programming java and java JaVa
Programming JAVA and JAVA JAVA
Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: rogramming java and java JaVa
rogramming JAVA and JAVA JAVA
Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: quit
間違いなく良い方法です。 1)あなたは文字列を間違って比較していますが、良い習慣ではありません。 2)StringBuilderを使用します。 –
これは「自分のコードをチェックする」サイトではありません。あなたのコードがうまくいかない場合は、バグを分離して、それについて質問してください。それがうまくいくなら、ここではお手伝いすることはできません。また、コードレビューstackexchangeサイトに質問を投稿することを検討したいかもしれません。しかし、そうすることを選択した場合でも、コードの改善方法などの具体的な指標を含め、質問を改善したいと考えています。 –
indexof()を使用して "java"を検索し、見つかったら。toUpperCase()を使用します。このコードはFARが長すぎます。 – IMustBeSomeone