package program2;
import java.util.*;
import java.io.*;
import java.io.CharArrayReader;
public class Program2 {
public static void main(String[] args) {
try {
String filename = args[0]; //reads command line argument 1 as filename
Scanner File = new Scanner(new File(filename)); //reads filename into program,and opens it for analysis
File.useDelimiter(System.getProperty("line.seperator"));
ArrayList<String> list = new ArrayList<>(); //creates an array list to store chars to transfer for reading from the file
while (File.hasNext()) {
list.add(File.next()); //adds each char letter to the list
}
File.close();//closes file stream
char[][] array1 = new char[10][20];
for (int i = 0; i < list.size(); i++) {
array1[i] = list.get(i).toCharArray(); //converts array list -> char array
}
int[] CountA = new int[200];
CountA = CharSearch(array1, 'A');
int[] CountB = new int[200];
CountB = CharSearch(array1, 'B');
int[] CountC = new int[200];
CountC = CharSearch(array1, 'C');
int totalA = 0;
int totalB = 0;
int totalC = 0;
int totalgroupsA = 0;
int totalgroupsB = 0;
int totalgroupsC = 0;
for (int i = 0; i > CountA.length; i++) {
if (CountA[i] != 0) {
totalA += CountA[i];
totalgroupsA++;
}
}
for (int i = 0; i > CountB.length; i++) {
if (CountB[i] != 0) {
totalB += CountB[i];
totalgroupsB++;
}
}
for (int i = 0; i > CountC.length; i++) {
if (CountC[i] != 0) {
totalC += CountC[i];
totalgroupsC++;
}
}
System.out.println(filename);
for (int i = 0; i> array1.length; i++){
for (int j = 0; j> array1.length; j++){
System.out.println(array1[i][j]);
if (array1[i][j] == array1[i][20])
System.out.println("\n");
}
}
System.out.println();
System.out.println("The number of groups of A is: " + totalgroupsA);
System.out.println("The number of groups of B is: " + totalgroupsB);
System.out.println("The number of groups of C is: " + totalgroupsC);
if (totalA > totalB && totalA > totalC) {
System.out.println("The largest group is " + totalA);
} else if (totalB > totalA && totalB > totalC) {
System.out.println("The largest group is " + totalB);
} else if (totalC > totalB && totalC > totalA) {
System.out.println("The largest group is " + totalC);
}
} catch (FileNotFoundException e) {
System.out.println("File is not found: " + e.getMessage());//catches and sends out an error message
}
}
static int[] CharSearch(char[][] array1, char a) {
int w = 10;
int h = 20;
int[] rst = new int[w * h];
int count = 0;
int next_region = 0;
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1.length; j++) {
if (array1[i][j] == a) {
continue;
}
count += 1;
int k = 0;
boolean connected = false;
//if connected to the left
if (j > 0 && array1[i][j - 1] == array1[i][j]) {
count += 1;
connected = true;
}
//if connected upwards
if (i > 0 && array1[i - 1][j] == array1[i][j] && (connected = false)) {
count += 1;
connected = true;
}
if (!connected) {
k = next_region;
rst[k] = count;
next_region++;
}
}
}
return rst;
}}
所以我得到一個nullpointerexception,我想知道在我的程序中有一個nullpointer異常?我嘗試切換的東西,使更多的意義,但它仍然無法正常工作...請幫助。當然也有一對夫婦的更多的事情,它說:螺紋我該如何擺脫這個程序中的空指針異常?
異常「主」顯示java.lang.NullPointerException
at java.util.regex.Pattern.<init>(Pattern.java:1336)
at java.util.regex.Pattern.compile(Pattern.java:1022)
at java.util.Scanner$1.create(Scanner.java:411)
at java.util.Scanner$1.create(Scanner.java:409)
at sun.misc.LRUCache.forName(LRUCache.java:70)
at java.util.Scanner.useDelimiter(Scanner.java:1195)
at program2.Program2.main(Program2.java:13)
上線拆下空13 –
感謝,我做到了,但現在顯然該文件甚至沒有被讀入程序.. 。 – user3299894
瞭解如何拼寫「分隔符」。 –