所以基本上我的代码应该计算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]);
}
}
答案 0 :(得分:0)
问题是在打印二维数组时,您只是跳过null
值。这会使正确打印在特定索引上的值混乱(使得d的直方图打印为d)。只需在空值中打印空格将使直方图正确打印。只需添加一个else
块来为空值打印一个空格,你就可以了。
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] + " ");
else
System.out.print(" ");
}
System.out.println();
}
}
<强> 输出: 强>
40 4 42 0 0 0 0 1 3 6 18 15 0 18 3 0 0 0 29 1 2 0 1 0 0 0
a b c d e f g h i j k l m n o p q r s t u v w x y z
* * * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * *
* * * * *
* * * * *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
*