简体   繁体   English

无法弄清楚我的Brackets_checker程序出了什么问题

[英]Can't figure out what is wrong with my Brackets_checker program

When I try to compile with Eclipse I have next errors: 当我尝试使用Eclipse进行编译时,出现下一个错误:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1416)
    at scanner_io.Brackets_checker.main(Brackets_checker.java:21)
package scanner_io;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Brackets_checker {

  public static void main(String[] args) {
    int k = 0;
    Scanner sc;
    char c ;
    boolean open = false;
    try {
      sc = new Scanner(new FileReader("data.txt"));
      int i = 0;
      c = sc.next().charAt(i);
      while (c != '\n') {
        c = sc.next().charAt(i);

        if (c == '(') {
          k++;
          open = true;
        }
        if (c == ')' && open == true) {
          k--;
          open = false;
        }

        i++;
      }

      sc.close();
      if (k == 0)
        System.out.println("OK !");
      else
        System.out.println("NOT OK !");
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }
}

Scanner#next() keeps eating elements, and you are getting a new element each time you invoke c = sc.next().charAt(i); Scanner#next()一直在吃元素,并且每次调用c = sc.next().charAt(i);时,您都会得到一个新元素c = sc.next().charAt(i);

This means that if you have for example the tokens Test is going wrong , you will produce the characters that will be assigned to c : T, s, i, n - this is NOT what you are after most likely. 这意味着,例如,如果您有令牌Test is going wrong ,您将产生将分配给c的字符: T, s, i, n这不是您最有可能想要的字符。

Instead, have a String variable currentToken , and iterate on its characters: 而是使用String变量currentToken ,并对其字符进行迭代:

For example: 例如:

String currentToken = sc.next()
for (int i = 0; i < currentToken.length(); i++) {
    c = currentToken.charAt(i)
}

You should also use Scanner#hasNext() before invoking next() , it's safer, also verify your returned String ( currentToken in above example) is not null . 您还应该使用Scanner#hasNext()调用之前next()它的安全,也验证了您的返回StringcurrentToken在上面的例子中)是不是null

this is wrong: 这是错误的:

c = sc.next().charAt(i);

You read a String from the file and get the i'th character from it. 您从文件中读取一个字符串,并从中获取第i个字符。 The next time you call c = sc.next().charAt(i) , you get the i'th character of the next String, which means you only read one character from each String and skip all the rest. 下次调用c = sc.next().charAt(i) ,将获得下一个String的第i个字符,这意味着您仅从每个String中读取了一个字符,而跳过了其余所有字符。

You should store sc.next() in a String variable and iterate over the characters of that String before you call sc.next() again. 您应该将sc.next()存储在String变量中,并在再次调用sc.next()之前遍历该String的字符。

And c != '\\n' is not a good stopping condition. 并且c != '\\n'不是一个好的停止条件。 You should check instead if the last call to sc.next() returned null. 相反,您应该检查对sc.next()的最后一次调用是否返回null。

Come to think of it, if you expect your entire input to be in one line, just call String s = sc.nextLine(); 想想看,如果您希望整个输入都在一行中,则只需调用String s = sc.nextLine(); one time and process the characters of s . 一次处理s的特征。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM