简体   繁体   中英

How to access input variable from another class? java

Trying to get the 1st class to recognize what the user inputs in the 2nd class. Any ideas as to what is going wrong here? The 2nd class works fine, but when i try to call 'input' from the main class, it says that 'input' cannot be resolved. Any suggestions and pointers much appreciated. Thanks for your time.

1st class:

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();
      }
   }
}

2nd class:

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;
     }
}

2nd class should look like:

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;
    }
}

And then you have to access the class in this way:

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();
           }
      }
 }

You are confused with two different things: Class and Object . The Object is an Instance of the Class . Without understanding this you cannot understand what is wrong here. Calling, for example Automata automata = new Automata() creates new Object of the class Automata .

"Extends" never helps you to get to the variables. It may help you to extend the current class and to use the methods that have been implemented in parent class, but you can never get to the pointers on the address spaces of that parent class. To access a variable of another object you should declare public getter method for that variable in that class .

I think you need to replace String s = input.readLine(); by String s = in4.readLine(); in Filter class. as readLine() is a method of BufferedReader Class

尝试像这样在输入中重命名BufferedReader的名称:

  BufferedReader input = new BufferedReader(new StringReader(automata.input));

I think you have a typo..

Change input in the 1st class to in4 .

'input' variable is declared in the 2nd class and you are trying to access it in the 1st class which is really impossible.

In class Filter , You do not have any member named input , due to which compile time exception is coming at input.readLine(); .

From the context of your program, it appears that in4 should be used instead of input .

public class Filter {

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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