user5446544
user5446544

Reputation:

System.out.printf not printing integer arguments

I'm new to java programming and I'm having trouble returning a method from another class. Both classes compile and run successfully. I'm able to call a simple int from one of the classes but when I want to calculate two input integers that the user has inputted I just get a blank space.

This is my calculations class

class calculations {
    public final int AGE = 53;
    public int numbers(int num1, int num2) {
        return num1 + num2;
    }
}

Here's my user class. I can get the AGE variable to print but the numbers() method doesn't return a value. It's just a blank space.

import java.util.Scanner;
class user {
    static Scanner input = new Scanner(System.in);

    public static void main(String args[]) {    
        int i1, i2;

        System.out.println("Enter int1: ");
        i1 = input.nextInt();

        System.out.println("Enter int2: ");
        i2 = input.nextInt();

        calculations kjc = new calculations();

        System.out.printf("Age = " + kjc.AGE);

        System.out.printf("\nints: " , i1, i2, kjc.numbers(i1, i2));

    }
}

If anyone can explain why System.out.printf("\nints: " , i1, i2, kjc.numbers(i1, i2)); doesn't print out the calculations of the two input ints.

Upvotes: 2

Views: 1509

Answers (3)

aliasm2k
aliasm2k

Reputation: 901

By the way, you could also use String.format function too.

String str = String.format("I was born in %d", 2015);
System.out.println(str);

Upvotes: 0

Abdul Rahman K
Abdul Rahman K

Reputation: 664

To be simple you can also print using:

System.out.println("\nints: ,"+ i1 +", "+i2+","+ kjc.numbers()");

Upvotes: 1

James Wierzba
James Wierzba

Reputation: 17548

You're using printf but you have zero format specifiers.

Try this

 System.out.printf("\nints: %d, %d, %d" , i1, i2, kjc.numbers ( i1,i2 ));

Basically, each argument after the first string is matched sequentially to each format specifier. %d is the format specifier for a decimal integer

Upvotes: 5

Related Questions