English 中文(简体)
在特定字符(包括空格)之间计数字符数
原标题:Count the number of characters between a specific character(Including spaces)

我有一个文件,就是这个格式:

City|the Location|the residence of the customer| the age of the customer| the first name of the customer|  

我需要读一下第一行, 确定符号"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

这就是我的代码:

`` `FileInputStream fs = new FileInputStream("C:/test.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
StringBuilder sb = new StringBuilder();

for(int i = 0; i < 0; i++){
br.readLine();
}
String line  = br.readLine();

System.out.println(line);

String[] words = line.split("|");
for (int i = 0; i < words.length; i++) {
    int counter = 0;
    if (words[i].length() >= 1) {
        for (int k = 0; k < words[i].length(); k++) {
            if (Character.isLetter(words[i].charAt(k)))
                counter++;
        }
        sb = new StringBuffer();
        sb.append(counter).append(" ");
    }
}
System.out.println(sb);
}

`` `

我对雅法非常新

最佳回答

尝试一下这样的东西:

String line = "City|the Location|the residence of the customer| the age of the customer| the first name of the customer|";
String[] split = line.split("\|"); //Note you need the \ as an escape for the regex Match
for (int i = 0; i < split.length; i++) {
  System.out.println("length of " + i + " is " + split[i].length());
}

产出 :

length of 0 is 4
length of 1 is 12
length of 2 is 29
length of 3 is 24
length of 4 is 31
问题回答

我只需要读一行就可以确定符号"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

string.split 需要一个正则表达式, 所以 需要逃跑 。 请使用 , 然后使用

words[i].length()

将会为您提供 符号之间的字符数 。

第一:

for(int i = 0; i < 0; i++){
  br.readLine();
}

只有当 低于 0 时才输入 < /code> 的 < code> 。

然后:

if (words[i].length() >= 1) { 

if 没有多大用处, 因为如果 words[i]. lentle() 是 0, 你将不会为 输入下一个 用于

最后,如果不测试它, 您可能想要测试字符是否为空格的字母“ 强” OR

更好地表现器使用字符串当量器, 而不是字符串.split (), 这里举一个例子 :

FileInputStream fs = new FileInputStream("C:/test.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
StringBuilder sb = new StringBuilder();

String line  = br.readLine();

System.out.println(line);

StringTokenizer tokenizer = new StringTokenizer(line, "|");
while (tokenizer.hasMoreTokens()) {
    String token = tokenizer.nextToken();
    sb.append(token.length()).append(" "); 
}
System.out.println(sb.toString());




相关问题
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 ...