英文:
How can you check multiple index using charAt() method in java?
问题
我正在尝试基于字符的索引比较来自.txt文件输入的两个字符串数组。
所以String1[i]不完整->示例:B_nana。
String2[j]是适合String1的单词->香蕉。
如果程序找到匹配的索引,例如->两者在索引1处都有"a",它会检查字符串长度,如果也匹配,它会替换`String1[i]=String2[j]`。
但每次在比较时`charAt()`索引超过字符串长度(其中一个字符串比另一个短),我都会收到`StringOutOfBoundException`。
所以我的问题是,是否有任何方法可以比较两个字符串不仅仅是一个索引,而是所有索引?
我已经尝试使用`string.substring(0, str.length - 1)`,但根本不起作用。
非常感谢任何建议,如果您需要更多信息来回答我的问题,请随时说明。
英文:
I am trying to compare two string-arrays from a .txt file input based on the index of a char.
So String1[i] is not complete -> example: B_nana.
String2[j] is the fitting word for string 1 -> Banana
If the program finds a matching index, for example -> both have "a" at index 1, it checks for the string length and if that matches aswell it replaces String1[i] = String2[j].
But everytime the charAt()
index exceeds the length of a string while comparing (one of the strings is shorter than the other), I get StringOutOfBoundException
.
So my question is, is there any way to compare not only one index of two strings but all of them?
I already tried using
string.substring(0, str.length -1)
but thats not working at all.
Grateful for any suggestions and if you need more information to answer my question please feel free to say so.
答案1
得分: 1
根据您的评论,我会执行以下操作:
- 构建有效单词列表(List
knownWords) - 构建带有缺口的单词列表(List
guessWords)
这里是一些伪代码
- 对于guessWords中的每个单词:
- while (!match && 在knownWords中还有更多单词)
- 将match设置为false
- 比较当前猜测单词的长度与knownWords中的第n个单词的长度
- 如果它们长度相同,逐个比较每个字符。如果已知单词的每个字符等于猜测单词的每个字符,或者如果猜测单词的字符是 _ ,则match = true
- while (!match && 在knownWords中还有更多单词)
这应该能帮助您朝正确的方向前进
示例源代码(Java)
package eu.webfarmr;
/**
* 该类接受两个列表(或数组),并尝试将第一个数组中的单词与第二个数组中的单词匹配
* 下划线 (_) 被视为缺失的字母。
* 作者:dbrossard
*
*/
public class WordMatcher {
private final static String[] knownWords = {"apple", "arcane", "batter", "butter", "banana"};
private final static String[] guessWords = {"a___e","a____e","__tte_","b_____"};
public static void main(String[] args) {
for (String guessWord : guessWords) {
boolean matchFound = false;
int i = 0;
while (!matchFound && i < knownWords.length) {
if (guessWord.length()==knownWords[i].length()) {
int j = 0;
while (j < guessWord.length() && (guessWord.charAt(j)==knownWords[i].charAt(j) || guessWord.charAt(j)=='_') ) {
j++;
}
matchFound = (j == guessWord.length());
if (matchFound) {
System.out.println(guessWord + " 匹配 " + knownWords[i]);
}
}
i++;
}
}
}
}
如果您想要找到所有匹配项,而不仅仅是第一个匹配项,请将 while (!matchFound && i < knownWords.length) {
更改为 while (i < knownWords.length) {
。
以下是示例输出:
a___e 匹配 apple
a____e 匹配 arcane
__tte_ 匹配 batter
__tte_ 匹配 butter
b_____ 匹配 batter
b_____ 匹配 butter
b_____ 匹配 banana
英文:
Based on your comment, I would do the following:
- Build a list of valid words (List<String> knownWords)
- Build a list of words with gaps (List<String> guessWords)
Here is some pseudo code
- For each word in guessWords:
- while (!match && has more words in knownWords)
- set match to false
- compare the length of the current guess word to the nth word in know words
- if they have the same length, compare each character. If each character from the known word is equal to each character of the guess word or if the guess word's character is _ then match = true
- while (!match && has more words in knownWords)
This should put you on the right track
Sample source code (Java)
package eu.webfarmr;
/**
* This class takes in 2 lists (or arrays) and tries to match words from the first array to words in the second
* the underscore (_) is considered to be a missing letter.
* @author dbrossard
*
*/
public class WordMatcher {
private final static String[] knownWords = {"apple", "arcane", "batter", "butter", "banana"};
private final static String[] guessWords = {"a___e","a____e","__tte_","b_____"};
public static void main(String[] args) {
for (String guessWord : guessWords) {
boolean matchFound = false;
int i = 0;
while (!matchFound && i < knownWords.length) {
if (guessWord.length()==knownWords[i].length()) {
int j = 0;
while (j < guessWord.length() && (guessWord.charAt(j)==knownWords[i].charAt(j) || guessWord.charAt(j)=='_') ) {
j++;
}
matchFound = (j == guessWord.length());
if (matchFound) {
System.out.println(guessWord + " matches " + knownWords[i]);
}
}
i++;
}
}
}
}
If you want to find all matches and not just the first one change while (!matchFound && i < knownWords.length) {
to while (i < knownWords.length) {
.
Here is sample output:
a___e matches apple
a____e matches arcane
__tte_ matches batter
__tte_ matches butter
b_____ matches batter
b_____ matches butter
b_____ matches banana
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论