1

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;
     }
}
2
  • you have no variable named as input in your main nor in your Filter class. Commented Jan 14, 2016 at 8:08
  • Yes, there is public static String input in the class Automata. Commented Jan 14, 2016 at 8:12

6 Answers 6

1

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();
           }
      }
 }
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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

Comments

1

Try to rename the name of BufferedReader in input like this:

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

Comments

1

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.