使用joptionpane打印来自句子的字母的索引位置

2hh7jdfx  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(612)

我需要要求用户输入一个句子,然后一个字母。然后程序应该打印出这个句子包含多少个字母。用户输入的指定字符的索引位置。我的问题是我不知道如何找到那个角色的位置。
注意:我已经在网上搜索了答案。

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");
        }
    }
}
t5fffqht

t5fffqht1#

把句子分成几个字母,然后通读一遍。如果匹配,则将其添加到将返回的某个列表中。

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);
cedebl8k

cedebl8k2#

如果只想显示字符所在的第一个索引,可以使用 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]

相关问题