English 中文(简体)
B. 在计票时的隔ing空间
原标题:Ignoring spaces while counting String tokens

我正在从事一个班子的家务劳动,我已经解决了几乎所有问题,但正在与一部分人作斗争。

就任务而言,我们应拟定一个方案,将计算在任何特定场合发出信件的频率,然后将信件的照片印制到青少年。 我写了该方案,它几乎是正确的,但我无法拿到地图来忽视白人空间。 似乎也发现了两种不同的白空间,一种是言语与我无法看到的另一种空间。

I ve Trial myString.replaceAll(”,“”; and myString.trim(); to在计算信函的频率之前尝试和消除白色空间,但每次仍算上两种白色空间。

任何见解或帮助都受到赞赏。 我可以这样做,但我不喜欢半衰期的项目。 该守则是:

import java.util.*;

public class LetterFrequency {
   public static void main( String[] args ) {

      Map< String, Integer > myMap = new HashMap< String, Integer >(); 

      createMap( myMap ); 
      displayMap( myMap );
   } 


   private static void createMap( Map< String, Integer > map ) {
      Scanner scanner = new Scanner( System.in ); 
      System.out.println( "Enter a string:" ); 
      String input = scanner.nextLine();

      System.out.println("String: "+input);

      String[] tokens = input.split("");


      for ( String token : tokens ) {
         String letter = token.toLowerCase();


         if ( map.containsKey( letter ) ) {
            int count = map.get( letter ); 
            map.put( letter, count + 1 );
         }
         else 
            map.put( letter, 1 );
      } 
   } 


   private static void displayMap( Map< String, Integer > map ) {      
      Set< String > keys = map.keySet(); 


      TreeSet< String > sortedKeys = new TreeSet< String >( keys );

      System.out.println( "
Map contains:
Key		Value" );


      for ( String key : sortedKeys )
         System.out.printf( "%-10s%10s
", key, map.get( key ) );

      System.out.printf( 
         "
size: %d
isEmpty: %b
", map.size(), map.isEmpty() );
   }
}
最佳回答

String.replaceAll should work. Keep in mind that String.replaceAll returns a String. So you have to use the string it returns to perform the rest of your computation.

例如,如果有:

String myString = "hello world";
myString.replaceAll(" ", "");

You d want:

String myNewString = myString.replaceAll(" ", "");

之后,<代码>my NewString将无空。

此外,你也可以通过利用你的特性来简化其特性。

for (int i = 0; i < input.length(); i++){
    char letter = input.charAt(i); 

This will fix your additional "whitespace being counted" problem. This is because when you call myString.split(""), the first element in the returned list is "" (an empty string).

问题回答

你们可以尝试<条码>myString.split(”/s+”;,以便消除所有白色空间,同时分裂。

正如@Kshitij Mehta所指出的,确保你使用这种方法的回报价值。 因此,你的法典希望这样做:

  String input = scanner.nextLine();
  System.out.println("String: "+input);
  input = input.trim();
  String[] tokens = input.split("\s+");

替代条目: 可在过滤你的输入,以在计算这些特性之前忽视任何非致命的特性(或在印刷结果之前照相过滤你的地图)。 E.g.

  for ( String token : tokens ) {
     if (Character.isLetter(token)) {
        String letter = token.toLowerCase();

        ...
     }
  } 

@Kshitij Mehta是正确的。

你们的问题是,不要通过建立白人空间来消除白人空间。

input = input.replace(" ", "");

另外,你的第二个问题是使用<代码>Strings而不是char(以及贵国地图的特征),以挽救这些信件(属性)。 单单是意外地利用Sting for char使java意外地发挥作用。

import java.util.*;

public class M {
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String s = sc.nextLine();
    s.toCharArray();
    String n = s.replaceAll(" ","");
    System.out.println(n.length());
  }
}

你可以尝试这样做。 我知道你已经这样做了。 但我认为这将有助于其他人。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签