我正在尝试删除一组中长度均匀的所有字符串.到目前为止,这是我的代码,但是我无法从增强型for循环中的迭代器中获取索引.
public static void removeEvenLength(Set<String> list) {
for (String s : list) {
if (s.length() % 2 == 0) {
list.remove(s);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想创建一个程序,在用户输入的字符串中给出字符,单词等的数量.要获得单词计数,我需要删除所有句点和逗号形成一个字符串.到目前为止我有这个:
import javax.swing.JOptionPane;
public class WordUtilities
{
public static void main(String args[])
{
{
String s = JOptionPane.showInputDialog("Enter in any text.");
int a = s.length();
String str = s.replaceAll(",", "");
String str1 = str.replaceAll(".", "");
System.out.println("Number of characters: " + a);
System.out.println(str1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但最后我只得到了这个:
Number of characters: (...)
Run Code Online (Sandbox Code Playgroud)
为什么不给我字符串没有逗号和句号?我需要修理什么?
我正在尝试为自己创建一个程序作为哈希图的教程。我要求用户输入文本,然后尝试将其拆分为哈希图,如果单词重复,则增加计数。这是我的程序:
import java.util.*;
import java.lang.*;
import javax.swing.JOptionPane;
import java.io.*;
public class TestingTables
{
public static void main(String args[])
{
{
String s = JOptionPane.showInputDialog("Enter any text.");
String[] splitted = s.split(" ");
HashMap hm = new HashMap();
int x;
for (int i=0; i<splitted.length ; i++) {
hm.put(splitted[i], i);
System.out.println(splitted[i] + " " + i);
if (hm.containsKey(splitted[i])) {
x = ((Integer)hm.get(splitted[i])).intValue();
hm.put(splitted[i], new Integer(x+1)); }
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我输入“随机随机随机”时,我得到:随机0随机1随机2
我需要更改什么才能得到:random 3另外,我是否需要使用迭代器打印出哈希图,还是我使用了OK?
我正在写一个计数程序的信.有没有办法使用一个案例来增加字符串中字母的数量,而不是使用26个案例.是否可以简化此计划?
import javax.swing.JOptionPane;
public class CountLetters
{
public static void main(String[] args)
{
{
String str = JOptionPane.showInputDialog("Enter any text.");
int count = 0;
String s = str.toLowerCase();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i)==('a')||s.charAt(i)=='b'||s.charAt(i)=='c'||s.charAt(i)=='d'||s.charAt(i)=='e'||s.charAt(i)=='f'||
s.charAt(i)=='g'||s.charAt(i)=='h'||s.charAt(i)=='i'||s.charAt(i)=='j'||s.charAt(i)=='k'||s.charAt(i)=='l'||
s.charAt(i)=='m'||s.charAt(i)=='n'||s.charAt(i)=='o'||s.charAt(i)=='p'||s.charAt(i)=='q'||s.charAt(i)=='r'||
s.charAt(i)=='s'||s.charAt(i)=='t'||s.charAt(i)=='u'||s.charAt(i)=='v'||s.charAt(i)=='w'||s.charAt(i)=='x'||
s.charAt(i)=='y'||s.charAt(i)=='z') {
count++;
}
}
System.out.println("There are " + count + " letters in the string you entered.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法简化这个程序,所以只有一个if条件,而不是26?
我有一个hashmap存储文本中出现的字符数.我正在尝试打印前三次出现,但打印不正确.
int max = 1000000000;
for (int i = 1; i <= 3; i++) {
for (Character key : list.keySet()) {
if (list.get(key) < max) {
max = list.get(key);
System.out.println(i + ": " + key + " " + list.get(key));
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)