Answer the question
In order to leave comments, you need to log in
How to fix Error Exception in thread "main"?
Error text:
Exception in thread "main" java.lang.NullPointerException
at TakeTheToken.main(TakeTheToken.java:26)
System.out.print(elements[0].token_type);
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TakeTheToken {
public String token;
public String token_type;
public TakeTheToken(String token, String token_type) {
this.token = token;
this.token_type = token_type;
}
public static TakeTheToken [] elements;
public static void main(String[] args) throws IOException {
String str = Open();
System.out.println(str);
addToken(str);
System.out.print(elements[0].token_type);
}
public static String Open() throws IOException {
File f = new File("D:\\1.txt");
FileReader fr = null;
try {
fr = new FileReader(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
String eachLine = null;
try {
eachLine = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (eachLine != null) {
sb.append(eachLine);
sb.append("\n");
try {
eachLine = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sb.toString().replaceAll("[\\n]", "");
}
public static void addToken(String str) {
char[] type = str.toCharArray();
int i = 0;
String word = "", number = "", charius = "";
while (i < str.length()) {
String typeChar = Character.toString(type[i]);
if (RegExp(typeChar) == 1) {//Если буквы
while (RegExp(typeChar) == 1) {
word += typeChar;
i++;
if (i < str.length()) {
typeChar = String.valueOf(type[i]);
} else break;
}
typeString(word, 1);
System.out.print(word);
word = "";
}
if (RegExp(typeChar) == 2) {//Если цифры
while (RegExp(typeChar) == 2) {
number += typeChar;
i++;
if (i < str.length()) {
typeChar = String.valueOf(type[i]);
} else break;
}
System.out.print(number);
number = "";
}
if (RegExp(typeChar) == 3) {
while (type[i] != ' ' && RegExp(typeChar) == 3) {
charius += typeChar;
if (i+1 < str.length()) {
typeChar = String.valueOf(type[i+1]);
} else break;
if (RegExp(typeChar) == 3) {
i++;
if (i < str.length()) {
typeChar = String.valueOf(type[i]);
}
}
}
System.out.print(charius);
charius = "";
}
i++;
}
}
public int c = 0;
public static void typeString(String str, int type) {
elements = new TakeTheToken[24];
if (type == 1) {
String[] s = {"int", "double", "float", "if", "else", "while", "for" };
for (int i = 0; i < s.length; i++) {
if (str.equals(s[i])) {
elements[i] = new TakeTheToken(str, "keywords");
i++;
}
}
}
}
public static int RegExp(String str) {
Pattern p = Pattern.compile("[a-zA-Z]");
Matcher m = p.matcher(str);
if (m.matches()) {
return 1;
}
p = Pattern.compile("[0-9]");
m = p.matcher(str);
if (m.matches()) {
return 2;
} else return 3;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question