我正在编码一个对消息进行编码的程序。输出应通过写下正整数N和符号来编码消息。您 可以通过在一行上连续写N次该符号来解码该消息。我的IDE中出现“无此类元素异常”错误。
import java.util.;
import java.io.;
public class Main
{
public static void main (String args[]) throws IOException
{
new Main ();
}
public Main () throws IOException
{
Scanner reader = new Scanner(System.in); // Reading from System.in
BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
int l = reader.nextInt(); //number of lines
int times [] = new int [l];
String string [] = new String [l];
for (int i = 0; i < l; i++) {
StringTokenizer st = new StringTokenizer(in.readLine());
String n = st.nextToken(); // <-- error occurs here
String n2 = st.nextToken();
int n1 = Integer.parseInt(n);
times[i] = n1;
string[i] = n2;
}
for (int i = 0; i < l; i++) {
function(times[i], string[i]);
if (i != (l-1))
System.out.println();
}
}
public void function (int a, String b) {
for (int i = 0; i < a; i++) {
System.out.print(b);
}
}
}
答案 0 :(得分:0)
问题出在这方面:
StringTokenizer st = new StringTokenizer(in.readLine());
String n = st.nextToken(); // <-- error occurs here
String n2 = st.nextToken();
int n1 = Integer.parseInt(n);
times[i] = n1;
string[i] = n2;
一种方法是这样做:
for (int i = 0; i < l; i++) {
String line = in.readLine();
String[] splitLine = line.split(" ");
int n1 = Integer.parseInt(splitLine[0]);
String n2 = splitLine[1];
times[i] = n1;
string[i] = n2;
}