0

I am new to java and i have 2 different Variables one is a double and one is a boolean. I am having difficulty printing them into the same line, this is my code.

class Main {
  public static void main(String[] args) {
    boolean isTrue;
    isTrue = false;
        double money;
        money = 99999.99;
System.out.println(isTrue, double);
  }
}

Thanks a lot if you answer

  • The code you posted does not compile. I think you need to replace `double` with `money` in the last line, i.e. `System.out.println(isTrue, double);` Is that your problem? – Abra Jul 17 '20 at 04:09
  • 3
    Does this answer your question? [How to print both bool and double in same line?](https://stackoverflow.com/questions/57846940/how-to-print-both-bool-and-double-in-same-line) – Shoaib Kakal Jul 17 '20 at 04:20

2 Answers2

0

Many ways to do this and if you visited the javadocs you would see that there is also System.out.print which does not do a line feed, so you could use that as

System.out.print(isTrue);
System.out.println(money);

Or you could use System.out.printf, or convert values into a String first

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Did you try to run the code from the question? If you did, did it work for you as written in the question? – Abra Jul 17 '20 at 04:10
  • @abra Does the code in the question does not even compile? How can I run it? I have answered the question *Printing 2 different variables in the same line Java VM* – Scary Wombat Jul 17 '20 at 04:13
  • A quote from the question: _I am having difficulty printing them into the same line_ Do you know what that difficulty is? If you don't then how can you claim to have answered the question? – Abra Jul 17 '20 at 04:15
0

There are few methods to do this, one is mentioned in the above answer and the other is below, that you can do this with a professional approach by using printf.

public static void main(String[] args) {
            boolean isTrue;
            isTrue = false;
            double money;
            money = 99999.99;
            System.out.printf("%b%.2f%n", isTrue, money);
        }
Shoaib Kakal
  • 1,090
  • 2
  • 16
  • 32
  • I have tried putting it in the compiler and it puts out a bug that's like exit status 1 Main.java:7: error: no suitable method found for println(double,boolean) System.out.println(money, isTrue); ^ method PrintStream.println() is not applicable (actual and formal argument lists differ in length) method PrintStream.println(boolean) is not applicable (actual and formal argument lists differ in length) method PrintStream.println(char) is not applicable (actual and formal argument lists differ in length) 1 error – Dhairya SHAH 08U12M Jul 17 '20 at 12:23