如何使用 IndexOf 和子字符串 Java 从字符串中提取多个单词?
How to Extract Multiple words from a string using IndexOf and substring Java?
我有一个通过系统导入的文件,现在卡住了。使用 while 循环和 if 语句,并且没有 Split() 方法的帮助,我如何首先使用扫描仪逐行读取文件?然后第二个我怎么能一个一个地抽出单词,当我抽出一个单词时,一个变量,countWords 必须增加一个,假设一个字符串中有 5 个单词,我需要 运行 通过循环 5 次,countWords 将变为 5。
这是我到目前为止的代码,有点蹩脚。
import java.util.Scanner;
import java.io.*;
class Assignmentfive
{
private static final String String = null;
public static void main(String[] args) throws FileNotFoundException
{
Scanner scan = new Scanner(new File("asgn5data.txt"));
int educationLevel = 0;
String fileRead = "";
int wordCount = 0;
while (scan.hasNext() && !fileRead.contains("."))
{
fileRead = scan.nextLine();
int index = fileRead.indexOf(" ");
String strA = fileRead.substring(index);
System.out.print(strA);
wordCount++;
}
我的代码还有更多内容,但只是注释掉了一些计算。
谢谢!
以下是我重构 while
循环以正确提取、打印和计算句子中所有单词的方法:
while (scan.hasNext()) {
int wordCount = 0;
int numChars = 0;
fileRead = scan.nextLine();
// Note: I add an extra space at the end of the input sentence
// so that the while loop will pick up on the last word.
if (fileRead.charAt(fileRead.length() - 1) == '.') {
fileRead = fileRead.substring(0, fileRead.length() - 1) + " ";
}
else {
fileRead = fileRead + " ";
}
int index = fileRead.indexOf(" ");
do {
String strA = fileRead.substring(0, index);
System.out.print(strA + " ");
fileRead = fileRead.substring(index+1, fileRead.length());
index = fileRead.indexOf(" ");
wordCount++;
numChars += strA.length();
} while (index != -1);
// here is your computation.
if (wordCount > 0) {
double result = (double)numChars / wordCount; // average length of words
result = Math.pow(result, 2.0); // square the average
result = wordCount * result; // multiply by number of words
System.out.println(result); // output this number
}
}
我通过将字符串 fileRead
硬编码为您的第一句话 The cat is black.
来测试此代码。我得到以下输出。
输出:
The
cat
is
black
我有一个通过系统导入的文件,现在卡住了。使用 while 循环和 if 语句,并且没有 Split() 方法的帮助,我如何首先使用扫描仪逐行读取文件?然后第二个我怎么能一个一个地抽出单词,当我抽出一个单词时,一个变量,countWords 必须增加一个,假设一个字符串中有 5 个单词,我需要 运行 通过循环 5 次,countWords 将变为 5。 这是我到目前为止的代码,有点蹩脚。
import java.util.Scanner;
import java.io.*;
class Assignmentfive
{
private static final String String = null;
public static void main(String[] args) throws FileNotFoundException
{
Scanner scan = new Scanner(new File("asgn5data.txt"));
int educationLevel = 0;
String fileRead = "";
int wordCount = 0;
while (scan.hasNext() && !fileRead.contains("."))
{
fileRead = scan.nextLine();
int index = fileRead.indexOf(" ");
String strA = fileRead.substring(index);
System.out.print(strA);
wordCount++;
}
我的代码还有更多内容,但只是注释掉了一些计算。 谢谢!
以下是我重构 while
循环以正确提取、打印和计算句子中所有单词的方法:
while (scan.hasNext()) {
int wordCount = 0;
int numChars = 0;
fileRead = scan.nextLine();
// Note: I add an extra space at the end of the input sentence
// so that the while loop will pick up on the last word.
if (fileRead.charAt(fileRead.length() - 1) == '.') {
fileRead = fileRead.substring(0, fileRead.length() - 1) + " ";
}
else {
fileRead = fileRead + " ";
}
int index = fileRead.indexOf(" ");
do {
String strA = fileRead.substring(0, index);
System.out.print(strA + " ");
fileRead = fileRead.substring(index+1, fileRead.length());
index = fileRead.indexOf(" ");
wordCount++;
numChars += strA.length();
} while (index != -1);
// here is your computation.
if (wordCount > 0) {
double result = (double)numChars / wordCount; // average length of words
result = Math.pow(result, 2.0); // square the average
result = wordCount * result; // multiply by number of words
System.out.println(result); // output this number
}
}
我通过将字符串 fileRead
硬编码为您的第一句话 The cat is black.
来测试此代码。我得到以下输出。
输出:
The
cat
is
black