Need help: input int from console and pass it into method in different class and do math

Posted by christophe on Stack Overflow See other posts from Stack Overflow or by christophe
Published on 2010-05-31T18:59:49Z Indexed on 2010/05/31 19:13 UTC
Read the original article Hit count: 155

Filed under:

i'm a beginner, Need help, Please!!!

I want to read optional number "a" from console and then store it in variable to use as passing to a different class (different .java file). and pint the sum separetely by optional inputting.

How do i code the 2 classes? thanks

/*
* DemoApp.java
*/

public class DemoApp {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int a;
        System.out.println("Input one of the following 3 numbers: 100, 200, 300");
        System.out.print("Enter: ");
        a = input.nextInt();
        TestApplication testapp = new TestApplication();
        testapp.test(a);

    }
}


/*
 * TestApplication.java
 *
 */

public class TestApplication {

    private int a; 

    public void test(int a) {
        this.a = a; // TODO: where to get the "a"? (entered by users from console)

        System.out.println("The number_a was passed in: "+a);
    }

    protected void printNum() throws Exception {

        int num;


        switch (a) {
        case 100:
            num = num + 10;
            break;

        case 200:
            num = num + 20;
            break;

        case 300:
            num = num + 30;
            break;

        default:
            // TODO: unexpected number input. throw();
            break;

        }

                System.out.println("I got a sum number"+num);

    }

}

© Stack Overflow or respective owner

Related posts about java