Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用indexOf方法搜索字符串_Java - Fatal编程技术网

Java 如何使用indexOf方法搜索字符串

Java 如何使用indexOf方法搜索字符串,java,Java,14.11(搜索字符串)编写一个应用程序,输入一行文本和一个搜索字符,并使用字符串方法indexOf确定字符在文本中出现的次数。 14.12(搜索字符串)根据练习14.11中的应用程序编写一个应用程序,该应用程序输入一行文本,并使用字符串方法indexOf确定文本中每个字母的出现总数。大写和小写字母应一起计算。将每个字母的总数存储在数组中,并在确定总数后以表格格式打印值。 我现在的问题是,如何确定单词中每个字母的出现率。例如,单词“出现”,每个字母在这个单词中出现了多少次 据我所知,我已经编写了

14.11(搜索字符串)编写一个应用程序,输入一行文本和一个搜索字符,并使用字符串方法indexOf确定字符在文本中出现的次数。 14.12(搜索字符串)根据练习14.11中的应用程序编写一个应用程序,该应用程序输入一行文本,并使用字符串方法indexOf确定文本中每个字母的出现总数。大写和小写字母应一起计算。将每个字母的总数存储在数组中,并在确定总数后以表格格式打印值。 我现在的问题是,如何确定单词中每个字母的出现率。例如,单词“出现”,每个字母在这个单词中出现了多少次

据我所知,我已经编写了我的代码,我的代码可以以表格形式显示字符并返回它们的索引。但这并不是问题所需要的

import java.util.*;

public class Searching
{
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    System.out.print("Enter Text: ");
    String text1 = input.nextLine();
    char[] text2 = text1.toCharArray();
    System.out.println("Character   Index");
    for(int i = 0; i < text2.length; i++)
    {
System.out.printf("%s%12d%n", text2[i],text1.indexOf(text2[i], i));
    }
}
} 
import java.util.*;
公共类搜索
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
系统输出打印(“输入文本:”);
字符串text1=input.nextLine();
char[]text2=text1.toCharArray();
System.out.println(“字符索引”);
对于(int i=0;i
我希望实际输出为表格格式。“发生”一词,我的代码应该显示每个字母及其发生的次数。 字母频率 o 1 C3 u 1 R2 E2
n1

使用HashMap可以吗?以下是14.12的解决方案:

Scanner in = new Scanner(System.in);
System.out.print("Enter text: ");
//So we only have to find lower case chars
String input = in.nextLine().toLowerCase();

//Add every single character in the word
//We use a LinkedHashMap instead of a HashMap to retain character order during printing.
LinkedHashMap<Character, Integer> characterCount = new LinkedHashMap<>();
for(int i = 0; i < input.length(); i++) {
    characterCount.put(input.charAt(i), 0);
}

//Iterate from every character ranging from a - z
//It would be more ideal to iterate only over the characters
//in the word. But your requirement asked to iterate over the alphabet.
for(int i = 'a'; i <= 'z'; i++) {
    //This is the character we are currently searching for
    Character searchingFor = (char)i;
    int startingIndex = 0; int foundIndex = 0;
    //Search for the character in the string FROM a specfic index (startingIndex in this case)
    //We update startingIndex to a bigger index in the string everytime we find the character.
    while((foundIndex = input.indexOf(searchingFor, startingIndex)) != -1) {
        //it must be the index+1 so the next time it checks after the found character.
        //without the +1, it's an infinite loop.
        startingIndex = foundIndex + 1;
        //Update count to the current value + 1
        characterCount.put(searchingFor, characterCount.get(searchingFor) + 1);
    }


}

//Print the results
for(Character c : characterCount.keySet()) {
    System.out.printf("%s%12d%n", c, characterCount.get(c));
}

in.close();

我不太确定你的问题是什么?听起来你希望有人为你编写代码
Enter text: occurence
o           1
c           3
u           1
r           1
e           2
n           1