【发布时间】:2017-10-10 07:28:35
【问题描述】:
所以基本上我的代码应该计算名为“input”的txt文件中每个字母的数量并相应地创建一个直方图。所以我所做的是我创建了一个方法,使整个文本文件为小写并使用ascii 代码在称为“formarray”的方法中计算每个字母的数量,该方法基本上返回一个大小为 26 的数组,其中每个位置包含文本文件中字母的出现次数(从位置零开始,对应于 ' a' 直到位置 25,即 z)。对于直方图,我创建了一个名为 str 的二维数组,其中第一行仅包含字母,而当某一列中的字母出现时,所有其他行都包含“*”。然而,尽管我命名为“ww”的数组包含每个字母出现的正确数量,但我没有得到正确的直方图形状。
package Assign;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class assign1 {
//
public static void print1d (int [] x) {
for (int i=0;i<x.length;i++) {
System.out.print(x[i]+" ");
}
System.out.println(" ");
}
public static void print1d (String [] x) {
for (int i=0;i<x.length;i++) {
System.out.print(x[i]+" ");
}
System.out.println("");
}
public static int [] formarray (String x) {
int [] thecounter = new int [26];
int counter=0;
int pointer=0;
for (int i=0;i<26;i++) {
for (int p=0;p<x.length();p++) {
int charint= x.charAt(p);
if ((charint<=122&charint>=97)) {
if ((i+97)==charint) {
counter=counter+1;
}
}
}
thecounter[i]=counter;
counter=0;
}
return thecounter;
}
public static int countthewords(String x) {//method to count words in a string
int countwords = 0;
if (x.length()== 0) {
return countwords;
} else {
while (x.length() != 0) {
if (x.charAt(0) != ' ') {
while (x.charAt(0) != ' ') {
x = x.substring(1);
}
countwords++;
} else
x = x.substring(1);
}
return countwords;
}
}
//
public static int countletters(String a) {
if (a.length()==0) {
return 0;
}
if (a.charAt(0)>=97&&a.charAt(0)<=122) {
return countletters(a.substring(1))+1;
}
else
return countletters(a.substring(1))+0;
}
public static void Print2d (String [][]a) {
for (int i=0;i<a.length;i++) {
for (int j=0;j<a[i].length;j++) {
if (a[i][j]!=null)
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
}
public static String LowerCaseAll(String w) {// a method to change everything to a lower case
String spare="";
for (int i =0;i<w.length();i++){
char t = (char) w.charAt(i);
if ((t>=65&t<=90)||(t<=122&t>=97)) {
char y =(char) w.charAt(i);
if (y<=90) {
y= (char) ((char) y+32);
spare=spare+y;
}else spare=spare+w.charAt(i);
}
else
spare=spare+w.charAt(i);
}
return spare;
}
//
public static String [] theletterstring() {
String [] r= new String [26];
for (int i=97;i<123;i++) {
char c =( (char) i);
String g = c+"";
r[i-97]=g;
}
return r;
}
public static void main(String[] args) throws IOException {
String [][]trial=new String [6][3];
for(int q=0;q<trial.length;q++) {
for (int z=0;z<trial[q].length;z++ ) {
if (q==3&&z==2)
trial [q][z]=null;
else
trial [q][z]="*";
}
}
//Print2d(trial);
int wordcount = 0;
BufferedReader br;
String cbr; // this string will contain the line im currently reading
String savecbr = "";// saves every line i read in this string so that at the end i will have
// everything in this string;
br = new BufferedReader(new FileReader("input.txt"));//read the file given which is "input.txt" in our case
cbr=br.readLine();
while (cbr!= null) {
savecbr = savecbr + " " +cbr;
cbr=br.readLine();
}
wordcount = countthewords(savecbr);
String savecbr1=LowerCaseAll(savecbr);
int ss= countletters(savecbr1);
int []ww =formarray(savecbr1);
String[][] str= new String [savecbr.length()] [26];
for (int t=0;t<26;t++) {
}
for (int f=0;f<26;f++) {
char c = (char) ((char)f+97);
str[0][f]=""+c;
}
//String[][] str= new String [savecbr.length()] [26];
for (int t=0;t<26;t++) {
for (int h=1;h<=ww[t];h++) {
str[h][t]= "*";
}
}
print1d(ww);
Print2d(str);
//System.out.println(ww[1]);
}
}
【问题讨论】:
-
如果我是对的,您正在尝试读取输入文件并尝试打印每个字母的出现次数。我说的对吗?
-
@Sridhar 完全正确,然后我试图创建一个直方图来可视化每个字母表的出现次数。但是直方图没有打印正确的结果
-
而且,您只打印直方图有问题吗?在
Print2d()方法 -
@Sridhar 我不确定问题出在 Print2d 还是最后两个 for 循环中
-
我添加了一个答案。它为我正确打印直方图。如果它解决了您的问题,请检查并接受答案。
标签: java arrays loops histogram counter