yuki
yuki

Reputation: 43

system.out.print outside of main method?

I am having trouble getting the "system.out.print" to work in the methods displayNumberPlus10, displayNumberPlus100, displayNumberPlus1000. Is there something I am doing wrong?

public class ArithmeticMethods {

    public static void main(String[] args){ 
        int cats = 46;
        int dogs = 25;

    }
        public static void displayNumberPlus10(){ 
        int cats = 46;
        int dogs = 25;

        System.out.print("There were" + cats + "now we will add 10 more."
                + (cats + 10) + ".");
        System.out.print("There were" + dogs + "now we will add 10 more."
                + (dogs + 10) + ".");
        }
    public static void displayNumberPlus100(){
        int cats = 46;
        int dogs = 25;
        System.out.print("There were" + cats + "now we will add 100 more "
                + (cats + 100) + ".");
        System.out.print("There were" + dogs + "now we will add 100 more "
                + (dogs + 100) + ".");
    }
    public static void displayNumberPlus1000(){
        int cats = 46;
        int dogs = 25;

        System.out.print("There were" + cats + "now we will add 1000 more "
                + (cats + 1000) + ".");
        System.out.print("There were" + dogs + "now we will add 1000 more."
                + (dogs + 1000) + ".");
}
}

Upvotes: 1

Views: 2374

Answers (3)

N Suhaib
N Suhaib

Reputation: 181

You have to call those methods in your main method. Add the following code in the main method.

displayNumberPlus10();
displayNumberPlus100();
displayNumberPlus1000();

Also, to enhance your code a little bit better, instead of writing this code in every method:

int cats = 46;
int dogs = 25;

You can just pass in cats and dogs in all your methods and pass in cats and dogs when you calling them in your main method also. So it would look something like this:

public class ArithmeticMethods {

public static void main(String[] args){ 
    int cats = 46;
    int dogs = 25;
    displayNumberPlus10(cats, dogs);
    displayNumberPlus100(cats, dogs);
    displayNumberPlus1000(cats, dogs);

}
    public static void displayNumberPlus10(int cats, int dogs){ 


    System.out.println("There were" + cats + "now we will add 10 more."
            + (cats + 10) + ".");
    System.out.println("There were" + dogs + "now we will add 10 more."
            + (dogs + 10) + ".");
    }
public static void displayNumberPlus100(int cats, int dogs){

    System.out.println("There were" + cats + "now we will add 100 more "
            + (cats + 100) + ".");
    System.out.println("There were" + dogs + "now we will add 100 more "
            + (dogs + 100) + ".");
}
public static void displayNumberPlus1000(int cats, int dogs){


    System.out.println("There were" + cats + "now we will add 1000 more "
            + (cats + 1000) + ".");
    System.out.println("There were" + dogs + "now we will add 1000 more."
            + (dogs + 1000) + ".");

} }

Hope that helps.

Upvotes: 2

ABHISHEK RANA
ABHISHEK RANA

Reputation: 327

You could try this

class ArithmeticMethods {

    public static void main(String[] args){ 
        int cats = 46;
        int dogs = 25;

    displayNumberPlus10(cats,dogs);
    displayNumberPlus100(cats,dogs);
    displayNumberPlus1000(cats,dogs);
    }
        public static void displayNumberPlus10(int c,int d){ 
        int cats = c;
        int dogs = d;

        System.out.print("There were" + cats + "now we will add 10 more."
                + (cats + 10) + ".");
        System.out.print("There were" + dogs + "now we will add 10 more."
                + (dogs + 10) + ".");
        }
    public static void displayNumberPlus100(int c,int d){
        int cats = c;
        int dogs = d;
        System.out.print("There were" + cats + "now we will add 100 more "
                + (cats + 100) + ".");
        System.out.print("There were" + dogs + "now we will add 100 more "
                + (dogs + 100) + ".");
    }
    public static void displayNumberPlus1000(int c,int d){
        int cats = c;
        int dogs = d;

        System.out.print("There were" + cats + "now we will add 1000 more "
                + (cats + 1000) + ".");
        System.out.print("There were" + dogs + "now we will add 1000 more."
                + (dogs + 1000) + ".");
}
}

Upvotes: 0

kirti
kirti

Reputation: 4609

You are not calling any of the methods. you need to call methods in the main method to execute them and then it will print

public class ArithmeticMethods {

    public static void main(String[] args){ 
        int cats = 46;
        int dogs = 25;
        //Call your methods as given below
        displayNumberPlus10();
        displayNumberPlus100();
        displayNumberPlus1000();    
    }

    public static void displayNumberPlus10(){ 
        int cats = 46;
        int dogs = 25;

        System.out.print("There were" + cats + "now we will add 10 more."
            + (cats + 10) + ".");
        System.out.print("There were" + dogs + "now we will add 10 more."
            + (dogs + 10) + ".");
    }

    public static void displayNumberPlus100(){
        int cats = 46;
        int dogs = 25;
        System.out.print("There were" + cats + "now we will add 100 more "
            + (cats + 100) + ".");
        System.out.print("There were" + dogs + "now we will add 100 more "
            + (dogs + 100) + ".");
    }

    public static void displayNumberPlus1000(){
        int cats = 46;
        int dogs = 25;

        System.out.print("There were" + cats + "now we will add 1000 more "
            + (cats + 1000) + ".");
        System.out.print("There were" + dogs + "now we will add 1000 more."
            + (dogs + 1000) + ".");
    } 
}

Upvotes: 0

Related Questions