Forum Stats

  • 3,838,578 Users
  • 2,262,383 Discussions
  • 7,900,690 Comments

Discussions

Java labs: How to format the output?

OTG-467455
OTG-467455 Member Posts: 506 Bronze Badge
edited Nov 21, 2017 10:02AM in Java Puzzle Ball

The java labs we have completed consisted of hacking the code to produce the desired output.

I am interested in tidying up the output to do the following:

  1. Strings and value in two separate columns
  2. values printing the "0.00" format
OTG-467455

Best Answer

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Nov 20, 2017 11:06PM Answer ✓

    I am using something like the one below. I don't know if that is going to help in

    I am interested in tidying up the output to do the following:Strings and value in two separate columns (???) 

     

    public void printDetails() {

          System.out.println(String.format("Account Owner: %s", accountOwner));

          System.out.println(String.format("Account Type: %s", accountType));

          DecimalFormat df = new DecimalFormat("$ #,##0.00");

          String formattedBal = df.format(balance);

          System.out.println(String.format("Balance for customer [%s]:  %s", accountOwner, formattedBal));

    }

    OTG-467455OTG-467455

Answers

  • NickR2600-Oracle
    NickR2600-Oracle Member Posts: 530 Employee
    edited Nov 20, 2017 3:59PM

    Sounds like you might be interested in Java Regular Expressions (regex) and pattern matching.  I've found regular expressions useful for formatting and searching Strings and numbers. There is a tutorial from Oracle (https://docs.oracle.com/javase/tutorial/essential/regex/ ).  I've also had luck googling for regular expression examples, since there's so many tricks, options, characters to keep track of.

    OTG-467455OTG-467455
  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Nov 20, 2017 11:06PM Answer ✓

    I am using something like the one below. I don't know if that is going to help in

    I am interested in tidying up the output to do the following:Strings and value in two separate columns (???) 

     

    public void printDetails() {

          System.out.println(String.format("Account Owner: %s", accountOwner));

          System.out.println(String.format("Account Type: %s", accountType));

          DecimalFormat df = new DecimalFormat("$ #,##0.00");

          String formattedBal = df.format(balance);

          System.out.println(String.format("Balance for customer [%s]:  %s", accountOwner, formattedBal));

    }

    OTG-467455OTG-467455