- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试编写一些代码来扫描输入文件中的回文,但它从每个单词而不是每一行中获取字符串。一个例子是赛车会显示为 racecar= palindrome 或 too hot to hoot = palindrome 但相反它会 go too= not a palindrome, hot= not a palindrome 等。
这是我目前正在做的读取文件的操作
File inputFile = new File( "c:/temp/palindromes.txt" );
Scanner inputScanner = new Scanner( inputFile );
while (inputScanner.hasNext())
{
dirtyString = inputScanner.next();
String cleanString = dirtyString.replaceAll("[^a-zA-Z]+", "");
int length = cleanString.length();
int i, begin, end, middle;
begin = 0;
end = length - 1;
middle = (begin + end)/2;
for (i = begin; i <= middle; i++) {
if (cleanString.charAt(begin) == cleanString.charAt(end)) {
begin++;
end--;
}
else {
break;
}
}
}
最佳答案
您需要进行以下更改
改变
while (inputScanner.hasNext()) // This will check the next token.
and
dirtyString = inputScanner.next(); // This will read the next token value.
到
while (inputScanner.hasNextLine()) // This will check the next line.
and dirtyString = inputScanner.nextLine(); // This will read the next line value.
inputScanner.next() 将读取下一个标记
inputScanner.nextLine() 将读取一行。
关于java - 逐行而不是逐字阅读文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20318872/
我正在尝试返回数组“seats”,它本质上应该从文本文件返回数据 - 15x30 网格“#”。我已经尝试了很多东西,但我感到很沮丧,因为我对 java 的经验很少。我的代码可以编译,但在调用该方法时无
对于任何输入的字符串,我们需要按任意顺序的单词匹配来查找 super 字符串。即输入字符串中的所有单词必须以任何顺序出现在输出字符串中。例如给定数据集:“字符串搜索”“Java 字符串搜索”“手动 C
我有一个文本文件,其中包含一些我想放入二维数组中的内容。该文本文件由等长的句子组成。如何将每个单词放入数组? 文本文件的例子是- This is stackoverflow I am user 这个文
我正在编写一个实用程序,它接受一个 .resx 文件并创建一个包含 .resx 文件中所有名称/值对属性的 javascript 对象。这一切都很好,直到 .resx 中的值之一是 该经销商接受电子订
我输入了大量的数学表达式和方程式,我想为每个表达式和方程式打印出 latex 表示形式。到目前为止,我已经尝试过 Sage 和 sympy,但棘手的部分是不对表达式中的术语重新排序。 所以,如果我的输
我正在尝试通过实现异步任务在 android (java) 中流式传输 chatgpt api 的响应(逐字),但我收到错误。我正在将 java 的 HTTPurlconnection 库与输入和输出
我正在尝试通过实现异步任务在 android (java) 中流式传输 chatgpt api 的响应(逐字),但我收到错误。我正在将 java 的 HTTPurlconnection 库与输入和输出
我是一名优秀的程序员,十分优秀!