AbDou CéTé
AbDou CéTé

Reputation: 33

How can I print this 2 Variables in the same println "System.out.println"

I have two int variables, abdou1 and abdou2 , I wish to print the values of these. I have tried below which does not work.

public class Math1 {
    public static void main (String args[]) {
        int abdou1 = 115;
        double abdou2 = 1122.176876; 
        System.out.println(abdou1, abdou2);
    }
}

Upvotes: 0

Views: 20909

Answers (7)

ROHIT kashyap
ROHIT kashyap

Reputation: 61

You can use printf or format eg

System.out.format("index = %d and %d",i,j);

System.out.printf("index = %d and %d",i,j);

Upvotes: 0

0xDEADBEEF
0xDEADBEEF

Reputation: 590

use a + sign and not a comma System.out.println(abdou1+" "+ abdou2);

Upvotes: 1

Subodh Karwa
Subodh Karwa

Reputation: 2725

This is very simple and can be achieved by using:

public static void main (String args[]) {

    int abdou1 = 115;
    double abdou2 = 1122.176876;

    System.out.println(abdou1 + " "  + abdou2);
}

The reason it works like this is that println interprets the object calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println(). Therefore, every parameter is treated as a String.

If you are printing objects of a custom class, you should override toString() to what you want to print.

Upvotes: -1

Rohit Bhardwaj
Rohit Bhardwaj

Reputation: 36

As printf method in java works same like C printf function so have to use format specifiers here to identify the data type.

public class Math1 {
public static void main (String args[]) {
    int abdou1 = 115;
    double abdou2 = 1122.176876; 
    System.out.println(String.format("%d %f", abdou1, abdou2));
}

You can use these Format Specifiers for different data types

  • %c or %C Display characters
  • %d Displays a decimal (base 10 ) integer
  • %e or %E Display a floating point number in exponential notation
  • %f Display a floating point value in decimal format
  • %s or %S Display Strings
  • %b or %B Display boolean values
  • %g (%G) float or double use %f or %e as required
  • %o int unsigned octal value
  • %p pointer address stored in pointer
  • %s array of char sequence of characters or String
  • %u int unsigned decimal
  • %x (%X) int unsigned hex value
  • %% Display a % sign

You can use whitespace characters which are

  • space ( ' ' )
  • tab ( '\t' )
  • carriage return ( '\r' )
  • newline ( '\n' )
  • ormfeed ( '\f' )

For more further explanation and examples with other data types you can go through this link.
Format Specifiers

Upvotes: 2

nits.kk
nits.kk

Reputation: 5316

System class holds a static reference to out which is of type PrintStream. If you see the signature of println() then you would see that it has various overloaded forms but each accepts single parameter. You can try like below.

public class Math1 {
    public static void main (String args[]) {
        int abdou1 = 115;
        double abdou2 = 1122.176876; 
        System.out.println(abdou1 + " "+ abdou2);
    }
}

Upvotes: 0

Zarul Izham
Zarul Izham

Reputation: 591

You can use

System.out.println(String.format("Int value: %d\nDouble value: %f", abdou1, abdou2));

Upvotes: 0

kamal
kamal

Reputation: 33

you should use "+" operator. Since this is a numerical value, if you directly use "+" between both the numbers, it will sum up. hence use a string separator like the one mentioned below

System.out.println(abdou1+" "+ abdou2);

Upvotes: 0

Related Questions