java - 如何从另一个类访问输入变量? java

标签 java

尝试让第 1 类识别用户在第 2 类中输入的内容。关于这里出了什么问题的任何想法?第二类工作正常,但是当我尝试从主类调用“输入”时,它说“输入”无法解析。非常感谢任何建议和指示。感谢您的宝贵时间。

第一类:

public class Filter {

   public static void main(String[] args) throws IOException { 
      BufferedReader in4 = new BufferedReader(new StringReader(automata.input));

      String s = input.readLine();
      while (automata.UserInput()==true){
         if (automata.accepts(s)) System.out.println(s); 
         s = input.readLine();
      }
   }
}

第二类:

public class automata extends Filter {

public static String input;
public static boolean UserInput() {

    System.out.println("Please enter test data: ");
    Scanner user_input = new Scanner(System.in);
    input = user_input.next();

    if (accepts(input) == true){
         System.out.print("works");
         return true;
     } else { 
       System.out.println("Problem");
       return false;
     }
}

最佳答案

第二类应该是这样的:

public class Automata { // we use upper case for class names

   public String input; // or better private and use a get-method

   public Automata() {} // constructor

   public boolean readUserInput() { // lower case here
        System.out.println("Please enter test data: ");
        Scanner user_input = new Scanner(System.in);
        String nextInput = user_input.next();
        input += nextInput; // otherwise you overwrite your current input
        /*if (accepts(input) == true){
            System.out.print("works");
            // return true;
        } else { 
            System.out.println("Problem");
            return false;
        }*/
        // It is a terrible idea to return every time a single word is read
        // rather read the whole String and then check if it is accepted
        if (accept(input)) // whole String is checked
            return true;
        return false;
    }
    // in case the input variable is private
    public String getInput() {
         return input;
    }
}

然后您必须以这种方式访问​​类:

public class Filter {

      public static void main(String[] args) throws IOException { 
           Automata automata = new Automata();
           if (automata.readUserInput()) {
                // BufferedReader in4 = new BufferedReader(new StringReader(automata.getInput())); or automata.input in case it is public
                // I don't understand why you want to read the input here again step by step
                // rather read the whole input here
                String userInput = automata.getInput();
           }
      }
 }

关于java - 如何从另一个类访问输入变量? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34784415/

相关文章:

java - 如何在 Heroku 上使用 reSTLet 在某些 URL 上强制使用 SSL

java - 如何使用 Java 从线路输入端口录制声音

java - 双击退出应用程序时出现问题

java - java中如何为不同的数据库创建多个数据库连接

java - 如何在java中访问zip文件中的文件和文件夹列表?

HTML "<meta http-equiv="Content-Type"content ="text/html; charset=ISO-8859-1"> "解析的Java正则表达式

java - 如何在 java 上显示所有对象

java - Spring mvc表单预填充: list with preferences

java - 在 JUnit 测试中启用 Spring 方法验证

java - DataOutputStream() VS DataOutputStream(新缓冲输出流())