Thursday, June 23, 2016

App Basics : Displaying Data to the Screen



When displaying data to a desktop computer screen the object System.out is used. For Android applications, the method is more complicated, the object out will be created to print data to the app screen as shown below:


out.println("Text to Display");

 
println - prints and then advances to the next line
print - prints without advancing to the next line

To display a blank line on the screen just use the format:


out.println();


The text or string you want to display must be contained within the "  " 

The string must be all on the same line

If you want to include " in your screen display they must be preceded by a \


out.println("I am \" INCLUDING \" the quotes");


To include a \ character just use a double \\


out.println("I am including the \\ here");
 

To include a tab use \t

out.println("I am including a tab \t here");
 

To go to a new line in the display add \n


out.println("Next line \n Line 2");


To add the value of a variable use + and the variable name
 
int total_Sum = 35+94;
 
out.println("I have a total of " + total_Sum);
 
out.println("Do you have " + total_Sum + " total?");


Displaying Formatted Data


printf - prints data that is formatted in a specific way

%d    - integer
%f    - a decimal number
%s    - a string
 

%5d    - an integer, 5 characters wide , right aligned
%-3d   - an integer, 3 characters wide, left aligned
%.5f    - a decimal number with 5 characters after the decimal
%7.3f   - a decimal number 7 characters wide, with 3 characters after the .

 
int display_int = 4596;
 
out.printf("I want to display an integer here %4d", display_int);




double display_dec=0.4596;
double display_dec2=0.3596;
 
out.printf(" %.3f percent of the total not %1.4f ", display_dec, display_dec2);


Decimal numbers are rounded if the full # of characters after the decimal is not used

The total width callout is a minimum width requirement, total width/number is not truncated

No comments:

Post a Comment