我需要让用户输入一个句子,然后输入一个字母。然后程序应该打印出句子中包含的字母数。还有的索引位置用户输入的指定字符。我的问题是我不知道如何找到那个角色的位置。
注意:我已经在网上搜索了答案。
import javax.swing.*;
public class Main {
public static void main(String[] args) {
String sentence; //Store the users senctence
String sentence2; //Stores the letter that the user wants to count.
int index;
sentence = JOptionPane.showInputDialog("Write a sentence");
sentence2 = JOptionPane.showInputDialog("Write a letter");
int sLenght = 0;
int countCha = sentence2.indexOf(sentence2);
if (sentence == null || sentence.equals(""))
JOptionPane.showMessageDialog(null, "You need to input a sentence to continue");
else {
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) != 1)
sLenght++;
}
JOptionPane.showMessageDialog(null, "The sentence contains" + " " + sLenght +
" " + "characters" + "n" + "Tecknet" + " " + sentence2 + " " + "occurs" + sentence.indexOf(sentence2) + " " + "times");
}
}
}
如果只想显示找到字符的第一个索引,可以使用String#indexOf(int ch)
。如果要显示字母在句子中出现的所有位置,可以使用String#indexOf(String str, int fromIndex)
。
演示:
public class Main {
public static void main(String[] args) {
String sentence = "Hello world!";
char ch = 'l';
int index = sentence.indexOf(ch);
if (index != -1) {
System.out.println("The first occurance of '" + ch + "' is at " + index);
} else {
System.out.println("The letter, '" + ch + "'does not exist in the sentence");
}
// All positions
System.out.println("All positions: ");
int fromIndex = 0, count = 0;
for (int i = 0; i < sentence.length(); i++) {
index = sentence.indexOf(ch, fromIndex);
if (index != -1) {
System.out.println("'" + ch + "' was found at " + index);
fromIndex = index + 1;
count++;
}
}
if (count == 0) {
System.out.println("The letter, '" + ch + "'does not exist in the sentence");
}
}
}
输出:
The first occurance of 'l' is at 2
All positions:
'l' was found at 2
'l' was found at 3
'l' was found at 9
或者,您可以使用String#charAt
:
public class Main {
public static void main(String[] args) {
String sentence = "Hello world!";
char ch = 'l';
int count = 0;
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == ch) {
System.out.println("'" + ch + "' was found at " + i);
count++;
}
}
if (count == 0) {
System.out.println("The letter, '" + ch + "'does not exist in the sentence");
}
}
}
输出:
'l' was found at 2
'l' was found at 3
'l' was found at 9
您也可以将所有位置添加到List<Integer>
并显示相同的位置。
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String sentence = "Hello world!";
char ch = 'l';
List<Integer> positions = new ArrayList<>();
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == ch) {
positions.add(i);
}
}
if (positions.size() == 0) {
System.out.println("The letter, '" + ch + "'does not exist in the sentence");
} else {
System.out.println("The positions where '" + ch + "' was found is/are " + positions);
}
}
}
输出:
The positions where 'l' was found is/are [2, 3, 9]
将句子拆分成单个字母并遍历它们。如果匹配,则将其添加到将返回的某个列表中。
public List<Integer> findOccurences(String letter, String sentence){
List<Integer> positions = new ArrayList<>();
String[] letters = sentence.split("");
for(int i = 0; i<letters.length; i++){
if(letter.equals(letters[i])){
positions.add(i);
}
}
return positions;
}
然后用调用它
List<Integer> occurenceList = findOccurences(sentence2, sentence);