java - 在将字符解析为 ascii 与比较我已经设置的分配值之间的循环中是否存在问题?
问题描述
我有一个程序从文本文件中获取信息,然后告诉用户使用了多少特定的字母、逗号、空格和句点。然而,当把它放出来时,一切都显示为 0。我的文本文件中有文本,我怀疑它可能与我的循环有关,但我不确定。
//read from a file
BufferedReader readFile= new BufferedReader(new FileReader("mytext.txt"));
//Counts record
while ((strInput=readFile.readLine())!=null)
{
intRecCount++;
}
character=new char[intRecCount];
strRecord=new String[intRecCount];
//open file
readFile=new BufferedReader (new FileReader ("mytext.txt"));
//input info
for (int i=0;i<strRecord.length;i++)
{
strRecord[i]=readFile.readLine();
}
//input valyes one by one per record
for (int i=0; i<strRecord.length;i++)
{
character[i]=strRecord[i].charAt(i);
intCompare=(int)character[i];
//for letters
for (int x=0;x<=25;x++)
{
if (intCompare==intUpLetter[x])
{
intCounter[x]++;
}
if (intCompare==intLowLetter[x])
{
intCounter[x]++;
}
}
// for the the 3 punction marks
for (int y=0;y<=2;y++)
{
if (intCompare==intPunct[y])
{
intCounter[25+y]++;
}
}
System.out.println("You have this many"+intCounter[0]+" A's");
System.out.println("You have this many"+intCounter[1]+" B's");
System.out.println("You have this many"+intCounter[2]+" C's");
System.out.println("You have this many"+intCounter[3]+" D's");
System.out.println("You have this many"+intCounter[4]+" E's");
System.out.println("You have this many"+intCounter[5]+" F's");
System.out.println("You have this many"+intCounter[6]+" G's");
System.out.println("You have this many"+intCounter[7]+" H's");
System.out.println("You have this many"+intCounter[8]+" I's");
System.out.println("You have this many"+intCounter[9]+" J's");
System.out.println("You have this many"+intCounter[10]+" K's");
System.out.println("You have this many"+intCounter[11]+" L's");
System.out.println("You have this many"+intCounter[12]+" M's");
System.out.println("You have this many"+intCounter[13]+" N's");
System.out.println("You have this many"+intCounter[14]+" O's");
System.out.println("You have this many"+intCounter[15]+" P's");
System.out.println("You have this many"+intCounter[16]+" Q's");
System.out.println("You have this many"+intCounter[17]+" R's");
System.out.println("You have this many"+intCounter[18]+" S's");
System.out.println("You have this many"+intCounter[19]+" T's");
System.out.println("You have this many"+intCounter[20]+" U's");
System.out.println("You have this many"+intCounter[21]+" V's");
System.out.println("You have this many"+intCounter[22]+" W's");
System.out.println("You have this many"+intCounter[23]+" X's");
System.out.println("You have this many"+intCounter[24]+" Y's");
System.out.println("You have this many"+intCounter[25]+" Z's");
System.out.println("You have this many"+intCounter[26]+" space's");
System.out.println("You have this many"+intCounter[27]+" comma's");
System.out.println("You have this many"+intCounter[28]+" periods'");
}
没有错误消息,但对于列出的每个字母和标点符号,输出应为 3。但所有显示为 0。
解决方案
您只查看第 i 行文本的第 i 个字符。您需要两个循环,一个循环遍历行(您有),一个循环遍历每行中的字符(您缺少)。
原来的:
//input info
for (int i=0;i<strRecord.length;i++)
{
strRecord[i]=readFile.readLine();
}
//input valyes one by one per record
for (int i=0; i<strRecord.length;i++)
{
character[i]=strRecord[i].charAt(i);
intCompare=(int)character[i];
:
}
你需要类似的东西
for (int i=0; i<strRecord.length;i++) {
for (int j=0; j<strRecord[i].length; j++) {
… deal with strRecord[i].charAt(j); …
}
}
推荐阅读
- android - React-native 应用程序在某些 Android 版本上不断崩溃
- django - Heroku/sendgrid 无法安装此插件
- php - 未发送 Microsoft Graph v1 webhook 通知
- html - Bootstrap: css: html: 将引导面板定位到页面的右上角
- swift - UITapGestureRecognizer 中标签问题的解释
- react-native - React Native:panGestureHandler 在重新渲染后停止工作
- c++ - 指向字符串文字的指针
- c# - 高分与 PlayerPref c#
- react-native - 如何修复错误:AnimatedValue:尝试将值设置为未定义 - React Native
- angular - 角度订阅变量不显示