Java Self-Study - Number and String Formatting Output

Keywords: Java less Eclipse Windows

Java uses printf or format for formatted output

Step 1: Format the output

If formatted output is not used, string joins are required, and splicing can be cumbersome if there are many variables
Use formatted output to be concise and clear

%s represents a string
%d denotes a number
%n means line break

package digit;
  
public class TestNumber {
  
    public static void main(String[] args) {
 
        String name ="Galen";
        int kill = 8;
        String title="Holy shit(dota), legendary(lol)";
         
        //Using + directly for string concatenation can make encoding feel tedious, less maintainable, and less readable
        String sentence = name+ " Continuous in progress " + kill + " After the second killing, we got " + title +" Title of";
         
        System.out.println(sentence);
         
        //Use formatted output
        //%s is a string,%d is a number,%n is a line break
        String sentenceFormat ="%s Continuous in progress %d After the second killing, we got %s Title of%n";
        System.out.printf(sentenceFormat,name,kill,title);
         
    }
}

Step 2: printf and format

printf and format work exactly the same way, viewing java source code through eclipse
As you can see, format is called directly in printf

package digit;
  
public class TestNumber {
  
    public static void main(String[] args) {
 
        String name ="Galen";
        int kill = 8;
        String title="Holy shit(dota), legendary(lol)";
         
        String sentenceFormat ="%s Continuous in progress %d After the second killing, we got %s Title of%n";
        //Format output using printf
        System.out.printf(sentenceFormat,name,kill,title);
        //Format output using format
        System.out.format(sentenceFormat,name,kill,title);
         
    }
}

Step 3: Line break

A line break is a new line -'\n'line break
A carriage return is to go back to the beginning of a line -'\r'carriage return
Knocking a carriage return in eclipse is actually a carriage return line break
Java is a cross-platform programming language, the same code can be used on different platforms, such as Windows,Linux,Mac
However, line breaks are different on different operating systems
(1) In DOS and Windows, each line ends with'\r\n';
(2) On Linux, only "\n" is at the end of each line;
(3) In a Mac system, only "\r" is at the end of each line.
In order to make the line breaks of the same java program behave the same in all operating systems,%n allows platform independent line breaks

package digit;
  
public class TestNumber {
  
    public static void main(String[] args) {
 
        System.out.printf("This is a line break%n");
        System.out.printf("This is a line break%n");
         
    }
}

Step 4: Total length, left alignment, complement 0, thousands separator, decimal places, localized expression
Other common formatting methods

package digit;
  
import java.util.Locale;
   
public class TestNumber {
   
    public static void main(String[] args) {
        int year = 2020;
        //Total length, left alignment, complement 0, thousands separator, decimal places, localized expression
          
        //Print Digits Directly
        System.out.format("%d%n",year);
        //Total length is 8, default right alignment
        System.out.format("%8d%n",year);
        //Total length is 8, left aligned
        System.out.format("%-8d%n",year);
        //Total length is 8, not enough to make up 0
        System.out.format("%08d%n",year);
        //thousands separator
        System.out.format("%,8d%n",year*10000);
  
        //decimals
        System.out.format("%.2f%n",Math.PI);
          
        //Thousands separator for different countries
        System.out.format(Locale.FRANCE,"%,.2f%n",Math.PI*10000);
        System.out.format(Locale.US,"%,.2f%n",Math.PI*10000);
        System.out.format(Locale.UK,"%,.2f%n",Math.PI*10000);
          
    }
}

Practice: Yellow Crane

Read string data with Scanner and format the output below

Jiangnan Leather Factory, the largest leather factory in Wenzhou, Zhejiang, closed down. Yellow Crane, the boss of eight eggs, ate, gambled, owed 350 million yuan and ran away with his little aunt! There was no way for us to get a wallet to pay for it! The original price was more than 100, 200 or 300 wallets. Now it only sells 20, totally only 20!You're not a human being, son of a bitch! We've worked hard for you for half a year. You don't pay your wages, you still have my sweat money and give me my sweat money!

Answer:

package digit;
 
import java.util.Locale;
import java.util.Scanner;
 
public class TestNumber {
 
    public static void main(String[] args) {
 
        Scanner s = new Scanner(System.in);
        System.out.println("Please enter a place name:");
        String location = s.nextLine();
        System.out.println("Please enter the company type:");
        String companyCategory = s.nextLine();
        System.out.println("Please enter a company name:");
        String companyName = s.nextLine();
        System.out.println("Please enter the name of your boss:");
        String bossName = s.nextLine();
        System.out.println("Please enter amount:");
        String money = s.nextLine();
        System.out.println("Please enter a product:");
        String product = s.nextLine();
        System.out.println("Please enter a price unit of measurement:");
        String unit = s.nextLine();
 
        String model = "%s Maximum%s%s Closed, boss of eight eggs%s Eat, drink, gamble, owe%s Billions,\r\n"
                + "Running with his aunt!There's nothing we can do. Hold it%s Pay offset!The original price is one%s More, Two%s Multiple, Three%s Many%s,\r\n"
                + "Now it's only twenty dollars, all in twenty!%s Monkey, you're not human!We have worked hard for you for half a year.\r\n"
                + "You don't pay me wages, you still give me sweat money, you give me sweat money!";       
        System.out.format(model, location,companyCategory,companyName,bossName,money,product,unit,unit,unit,product,bossName);
         
    }
}

Posted by ThEMakeR on Sun, 29 Sep 2019 20:47:34 -0700