TOPIC

Java - Runtime error

Carlos Higa asked 1 year ago

public class Main {

    public static void main(String[] args) throws IOException {

        String filePath = "C:/Temp/expressoes.txt";

        File file = new File(filePath);
        Scanner scanner = null;     

        try {
            scanner = new Scanner(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        String expression = "";
        List<String> validExpressions = new ArrayList<String>();
        int qtyValidExpressions = 0;

        //Valida a quantidade e tamanho das expressoes
        while(scanner.hasNext()){
            expression = scanner.nextLine();

            if(expression.length() >= 1 && expression.length() <= 1000){
                qtyValidExpressions++;
                validExpressions.add(expression);
            } else if(expression.length() > 1000) {
                return;
            }
        }

        //Valida as expressoes
        if(qtyValidExpressions >= 1 && qtyValidExpressions <= 10000){
            Stack<Character> stack = new Stack<Character>();
            String answer = "";
            boolean alreadyIncorrectFlag = false;

            for(int i=0; i < qtyValidExpressions; i++){
                for(int j=0; j < validExpressions.get(i).length(); j++){
                    if(validExpressions.get(i).charAt(j) == ')' && stack.isEmpty()) {
                        answer = "incorrect";
                        alreadyIncorrectFlag = true;
                        j = validExpressions.get(i).length();
                    } else if(validExpressions.get(i).charAt(j) == '(') {
                        stack.push(validExpressions.get(i).charAt(j));
                    } else if(validExpressions.get(i).charAt(j) == ')') {
                        stack.pop();
                    }           
                }

                if(alreadyIncorrectFlag == false) {
                    if(stack.isEmpty()) {
                        answer = "correct";
                    } else {
                        answer = "incorrect";
                    }                       
                }

                //Exibe a resposta, limpa a pilha e a flag
                System.out.println(answer);

                stack.clear();
                alreadyIncorrectFlag = false;
            }
        }

    }

}

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

  • Carlos Higa replied 1 year ago

    Caros, por gentileza, alguém consegue identificar o motivo de eu estar recebendo Runtime Error?

  • Carlos Higa replied 1 year ago

    Fiz uma pequena modificação no código, usando agora o método hasNextLine() do Scanner para também ler as linhas em branco, mas continuo recebendo o mesmo erro de Runtime Error.

    Qual seria a diferença em runtime e compilation error?

    Desde já, agradeço

  • Carlos Higa replied 1 year ago

    public static void main(String[] args) throws IOException {
    
        String filePath = "C:/Temp/expressoes.txt";
    
        File file = new File(filePath);
        Scanner scanner = null;     
    
        try {
            scanner = new Scanner(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    
        String expression = "";
        List<String> validExpressions = new ArrayList<String>();
        int qtyValidExpressions = 0;
    
        //Valida a quantidade e tamanho das expressoes
        while(scanner.hasNextLine()){
            expression = scanner.nextLine();
    
            if(expression.length() <= 1000){
                qtyValidExpressions++;
                validExpressions.add(expression);
            } else if(expression.length() > 1000) {
                return;
            }
        }
    
        //Valida as expressoes
        if(qtyValidExpressions >= 1 && qtyValidExpressions <= 10000){
            Stack<Character> stack = new Stack<Character>();
            String answer = "";
            boolean alreadyIncorrectFlag = false;
    
            for(int i=0; i < qtyValidExpressions; i++){
                for(int j=0; j < validExpressions.get(i).length(); j++){
                    if(validExpressions.get(i).charAt(j) == ')' && stack.isEmpty()) {
                        answer = "incorrect";
                        alreadyIncorrectFlag = true;
                        j = validExpressions.get(i).length();
                    } else if(validExpressions.get(i).charAt(j) == '(') {
                        stack.push(validExpressions.get(i).charAt(j));
                    } else if(validExpressions.get(i).charAt(j) == ')') {
                        stack.pop();
                    }           
                }
    
                if(alreadyIncorrectFlag == false) {
                    if(stack.isEmpty()) {
                        answer = "correct";
                    } else {
                        answer = "incorrect";
                    }                       
                }
    
                //Exibe a resposta, limpa a pilha e a flag
                System.out.println(answer);
    
                stack.clear();
                alreadyIncorrectFlag = false;
            }
        }
    }