Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Formatting --- Combining two separate statements into single line

Right now the lines inside the while loop "You will be charged a fee of 10 dollars " and " Would you like to close your account?" appear on separate lines like...

"you will be charged a fee of 10 dollars."
"Would you like instructions?"

How do I change the formatting of my code so both statements are printed on a single line. The program should still work the same. The answer should appear as

"You will be charged a fee of 10 dollars. Would you like to close your account?"

import acm.program.*;

public class BankAccount extends ConsoleProgram {
    public void run() {

        int bankRoll = 50;

        while(bankRoll > 0) {
            println("Currently your balance is " + bankRoll + " . ");
            println("You will be charged a fee of 10 dollars."); 
            String closeAct = readLine("Would you like to close your account?");
            if(closeAct.equals("yes")) {
                break;
            }
            bankroll -= 10;
        } /* end of while loop */

        println("your account is now closed.");

    }
}
like image 424
Jessica M. Avatar asked Apr 15 '26 09:04

Jessica M.


2 Answers

You have to use print instead of println.

println is like print, but at the end of the String, it adds a "\n" which is the newline character.

If you want to make more than one line, you can also use print and add a "\n" where you want to have a break.

For instance:

print("This is an example with one line")
print("This is still in the same line as the text ahead.")

if you want to make 2 lines, but have 1 one String, you can also

print("This is an example with 2 lines" + "\n" + "but only 1 String")

println would make the "\n" at the end of the line so like before

println("This is an example with two lines") // Here is an automatically added "\n"
print ("This text is in the next line")
like image 135
Crusader633 Avatar answered Apr 17 '26 23:04

Crusader633


Use print rather than println where appropriate to suppress the newline character.

like image 25
Bathsheba Avatar answered Apr 17 '26 21:04

Bathsheba