Reading same input twice, in two different methods

DJmendy

I have two separate methods, in two different classes. I want them both to read the same line of input and check for different things. One looks for Instructions like "make me a coffee" and the other looks for different keywords like "please" and "thank you" (these effect how the programme responds to me):

public class Class1(){

public void PleaseAndThankYous(){
Scanner scanner1 = new Scanner(System.in)
input1 = Scanner1.nextLine();

if (input1.contains//blah blah blah blah...


public class Intructions(){

public void Method2(){
Scanner scanner2 = new Scanner(System.in)
input2 = Scanner2.nextLine();

if (input1.contains//blah blah blah blah...

and then I'm calling them in my main string, just to test them:

System.out.println("this is a test, ask me something")
obj.PleaseAndThankYous();
obj.Intructions();

and my console prints out like this:

this is a test, ask me something  //(out put string)
make me a coffee please         //PleaseAndThankYous() reads this
make me a coffee please         // Intructions() reads this;
Making you a coffee, Sir.   // (response)

I understand whats going on, but I can't think of another way. I've also tried using the same Scanner, with different Strings and it still didn't work. How can I make it so that both methods read my first line of input and I don't have to type everything twice? Thanks!

nhouser9

Right now you have two methods like this:

public void method() {
    Scanner scanner = new Scanner(System.in)
    input = Scanner.nextLine();

    if (input.contains //blah blah blah blah...

Change them both so they take an argument:

public void method(String input) {        
    if (input.contains //blah blah blah blah...

Then in your main method pass the input you want them to read, so instead of:

method1();
method2();

Use:

Scanner in = new Scanner(System.in);
String input = Scanner.nextLine();
method1(input);
method2(input);

Basically, getting the same string multiple times from a Scanner is not really supported. But you can very easily get the value once and then use it multiple times in different places by passing it as an argument.

This is also better in several other respects - it will increase performance (since you only declare a scanner and read from it once) and it makes your code more modular, as you can have a class for handling input and another class for processing it, instead of doing both in multiple places.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Refactoring two methods into one that have the same fields but different names

分類Dev

Python giving me two different results while performing the same operation on the same objects twice

分類Dev

Is it possible to have two catches with the same Exception type from two different methods?

分類Dev

How can i implement two JNI methods with same name but different Params?

分類Dev

Read Same Text File in Different Methods

分類Dev

ng tags input not allowing same text twice to be entered in the text box

分類Dev

Java inheritance - Two classes that use the same methods

分類Dev

Input cycle loops twice when given two values

分類Dev

Simplifying LINQ - Sum of a Col twice with two different where clause

分類Dev

Can I use the same name for different methods in different interface?

分類Dev

Why do these two apis for the same site not have the same methods?

分類Dev

regex that matches for two different places on input

分類Dev

Two different variables getting same value

分類Dev

Subtract two dataframe with the same name different index

分類Dev

Why two different buffer for a same string?

分類Dev

Merge same columns in two different using python

分類Dev

Two routers, same network, different SSID and password

分類Dev

Using two different networks at the same time

分類Dev

use the same method for two different events?

分類Dev

Reading from two different files and printing to third one

分類Dev

Have a Go function accept different structs as input with methods

分類Dev

For gtest, how to mock methods which have the same name, but different types

分類Dev

How to make use of two buttons with two different actions in the same form?

分類Dev

Combine two fields of two different log lines in same index pattern

分類Dev

Rails ActiveRecord model has_one twice to the same model with different foreign keys

分類Dev

Is there a Better way to handle Same callback function but different input params?

分類Dev

Media query has two different results for the footer on two different pages even though the codes the same?

分類Dev

How to implement two different interfaces with the same method signature

分類Dev

Vue.js - two different components on same route

Related 関連記事

  1. 1

    Refactoring two methods into one that have the same fields but different names

  2. 2

    Python giving me two different results while performing the same operation on the same objects twice

  3. 3

    Is it possible to have two catches with the same Exception type from two different methods?

  4. 4

    How can i implement two JNI methods with same name but different Params?

  5. 5

    Read Same Text File in Different Methods

  6. 6

    ng tags input not allowing same text twice to be entered in the text box

  7. 7

    Java inheritance - Two classes that use the same methods

  8. 8

    Input cycle loops twice when given two values

  9. 9

    Simplifying LINQ - Sum of a Col twice with two different where clause

  10. 10

    Can I use the same name for different methods in different interface?

  11. 11

    Why do these two apis for the same site not have the same methods?

  12. 12

    regex that matches for two different places on input

  13. 13

    Two different variables getting same value

  14. 14

    Subtract two dataframe with the same name different index

  15. 15

    Why two different buffer for a same string?

  16. 16

    Merge same columns in two different using python

  17. 17

    Two routers, same network, different SSID and password

  18. 18

    Using two different networks at the same time

  19. 19

    use the same method for two different events?

  20. 20

    Reading from two different files and printing to third one

  21. 21

    Have a Go function accept different structs as input with methods

  22. 22

    For gtest, how to mock methods which have the same name, but different types

  23. 23

    How to make use of two buttons with two different actions in the same form?

  24. 24

    Combine two fields of two different log lines in same index pattern

  25. 25

    Rails ActiveRecord model has_one twice to the same model with different foreign keys

  26. 26

    Is there a Better way to handle Same callback function but different input params?

  27. 27

    Media query has two different results for the footer on two different pages even though the codes the same?

  28. 28

    How to implement two different interfaces with the same method signature

  29. 29

    Vue.js - two different components on same route

ホットタグ

アーカイブ