TOPIC

Runtime error

Nir Rifat asked 1 year ago

When I run into my computer with random cases it works, but when I run URI from Runtime error, can anyone help me?

import java.io.IOException; import java.util.Scanner; public class Test { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); int n = in.nextInt(); while (in.hasNext()) { int num1[] = new int[n]; int num2[] = new int[n]; int num3[] = new int[n]; String temp[] = new String[n]; String name[] = new String[n]; boolean rst[] = new boolean[n]; int index[] = new int[n]; char op[] = new char[n]; for (int i = 0; i < n; i++) { num1[i] = in.nextInt(); temp[i] = in.next(); char tem[] = temp[i].toCharArray(); int j = 0; String s = ""; while (tem[j] != '=') { s += Character.toString(tem[j]); j++; } num2[i] = Integer.parseInt(s); j++; s = ""; while (tem.length > j) { s += Character.toString(tem[j]); j++; }

            num3[i] = Integer.parseInt(s);
        }
        int c = 0;
        for (int i = 0; i < n; i++) {
            name[i] = in.next();
            index[i] = in.nextInt();
            op[i] = in.next().charAt(0);

        }
        for (int i = 0; i < n; i++) {
            rst[i] = result(num1[index[i] - 1], num2[index[i] - 1], num3[index[i] - 1], op[i]);
            if (!rst[i]) {
                c++;
            }
        }

        if (c == n) {
            System.out.println("None Shall Pass!");
        } else if ((c == 0)) {
            System.out.println("You Shall All Pass!");
        } else {
            String newname[] = new String[c];
            int q = 0;
            for (int i = 0; i < n; i++) {
                if (!rst[i]) {
                    newname[q] = name[i];
                    q++;
                }
            }
            for (int i = 0; i < c - 1; i++) {
                for (int j = i + 1; j < c; j++) {
                    if (newname[i].compareTo(newname[j]) > 0) {
                        String nn = newname[i];
                        newname[i] = newname[j];
                        newname[j] = nn;
                    }
                }
            }
            for (int i = 0; i < c; i++) {
                if (i == 0) {
                    System.out.print(newname[i]);
                } else {
                    System.out.print(" " + newname[i]);
                }
            }
            System.out.println("");
        }

        n = in.nextInt();
    }
}

static boolean result(int a, int b, int c, char op) {
    switch (op) {
        case '+':
            return a + b == c;
        case '-':
            return a - b == c;
        case '*':
            return a * b == c;
        default:
            return a + b != c && a - b != c && a * b != c;
    }
}

}

This topic has not been answered yet. Be the first!

Remember not post solutions. Your post may be reviewed by our moderators.