String & I/O
Forbedre dine utskrifter
Av Gard Inge Rosvold
Innhold
- String (formatering)
- Metoder og returverdi
- I/O, mer om O
Vanlig
String & formatering
int km = 360;
double kmPrHr = 57.8;
double hours = km / kmPrHr;
System.out.println("It takes about " + hours + " hours to drive "
+ km + "km with " + kmPrHr + "km/hr average speed.");
// Result
It takes about 6.228373702422146 hours to drive 360km with 57.8km/hr average speed.
Formatert
int km = 360;
double kmPrHr = 57.8;
double hours = km / kmPrHr;
System.out.format("It takes about %.2f hours to drive %dkm" +
"with %.1fkm/hr average speed.\n",
hours, km, kmPrHr);
// Result
It takes about 6.23 hours to drive 360km with 57.8km/hr average speed.
String & formatering
De vanligste formateringene
- String %s
- int %d
- double/float %f
Mer info ved googling: "String format java"
øverste resultat 'class Formatter'
String & formatering
Tabeller helt basic
int[] hoursWorked = {4, 5, 0, 4, 6, 2, 3};
String[] weekdays = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
for (int i=0; i<weekdays.length; i++) {
System.out.println(weekdays[i] + ": " + hoursWorked[i]);
}
// Result
Mon: 4
Tue: 5
Wed: 0
Thu: 4
Fri: 6
Sat: 2
Sun: 3
String & formatering
Tabeller litt bedre
int[] hoursWorked = {4, 5, 0, 4, 6, 2, 3};
String[] weekdays = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
// Better ?
for (int i=0; i<weekdays.length; i++)
System.out.print(weekdays[i] + " ");
System.out.println();
for (int i=0; i<hoursWorked.length; i++)
System.out.print(" " + hoursWorked + " ");
System.out.println();
// Result
Mon Tue Wed Thu Fri Sat Sun
4 5 0 4 6 2 3
String & formatering
Flere formateringsmuligheter
%-4.2f
'-' betyr at det skal left-alignes
'4' betyr at det skal være minst 4 plasser venstre for punktum
'.2' betyr at det skal være minst 2 plasser etter punktum
'f' betyr at dette er en float
String & formatering
Tabeller formatert
int[] hoursWorked = {4, 5, 0, 4, 6, 2, 3};
String[] weekdays = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
// Better ?
for (int i=0; i<weekdays.length; i++)
System.out.format("%3s ", weekdays[i]);
System.out.println();
for (int i=0; i<hoursWorked.length; i++)
System.out.format("%3d ", hoursWorked[i]);
System.out.println();
// Result
Mon Tue Wed Thu Fri Sat Sun
4 5 0 4 6 2 3
String & formatering
Tabeller med String
int[] hoursWorked = {4, 5, 0, 4, 6, 2, 3};
String[] weekdays = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
// Even Better ?
String descriptionLine = "";
String workLine = "";
for (int i=0; i<weekdays.length; i++) {
descriptionLine += String.format("%3s ", weekdays[i]);
workLine += String.format("%3d ", hoursWorked[i]);
}
System.out.format("%s\n%s\n", descriptionLine, workLine);
// Result
Mon Tue Wed Thu Fri Sat Sun
4 5 0 4 6 2 3
String & formatering
Vanlig
Metoder og returverdi
public void printArray() {
int[] hoursWorked = {4, 5, 0, 4, 6, 2, 3};
String[] weekdays = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
String descriptionLine = "";
String workLine = "";
for (int i=0; i<weekdays.length;
descriptionLine += String.format("%3s ", weekdays[
workLine += String.format("%3d ", hoursWorked[i]);
}
System.out.format("%s\n%s\n", descriptionLine, workLine);
}
Litt videre
Metoder og returverdi
public String printArray() {
int[] hoursWorked = {4, 5, 0, 4, 6, 2, 3};
String[] weekdays = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
String descriptionLine = "";
String workLine = "";
for (int i=0; i<weekdays.length;
descriptionLine += String.format("%3s ", weekdays[
workLine += String.format("%3d ", hoursWorked[i]);
}
return String.format("%s\n%s\n", descriptionLine, workLine);
}
public void execute() {
String arrayPrint = printArray();
System.out.print(arrayPrint);
}
// result same as before
Mon Tue Wed Thu Fri Sat Sun
4 5 0 4 6 2 3
Live eksempel
Metoder og returverdi
/**
* Generates the graph of tree from this node forward.
*
* @return the String representing recursive version of graph
*/
protected String generateGraph() {
String result = String.format("%c", this.c);
if (childrenCount == 1) {
result += String.format("%s", onlyChild.generateGraph());
}
else {
int counter = 0;
for (int i=0; (i < children.length && counter < childrenCount); i++) {
if (children[i] != null) {
counter++;
result += String.format("(%s)", children[i].generateGraph());
}
}
}
return result;
}
// Example result
internet (internet)
interview (inter(net)(view))
internally (inter(n(ally)(et))(view))
algorithm (algorithm)
all (al(gorithm)(l))
web (web)
world (w(eb)(orld))
Fordeler
- Formatert String +
- Metode som returnerer denne =
- En fin tekst å benytte
- Kan printes til skjerm eller til fil
- 1 metode < flere muligheter
I/O, mer om O
Bruker bestemmer
private static void executeCommand(String cmd) throws Exception {
String[] splitCmd = cmd.split(" ");
switch (splitCmd[0].toLowerCase()) {
case "export":
if (splitCmd.length < 3) {
print("Not enough arguments for 'export', see usage\n");
return;
}
String outfilename = splitCmd[2];
PrintWriter out = new PrintWriter(new File(outfilename));
switch (splitCmd[1].toLowerCase()) {
case "print": case "p":
out.write(printArray());
break;
case "help": case "h":
out.write(usage());
break;
}
out.close();
break;
case "print": case "p":
print(printArray());
break;
case "help": case "h":
print(usage());
break;
}
}
I/O, mer om O
Grublegruppe: String & I/O
By gardir
Grublegruppe: String & I/O
Her er det slides om String formatering og I/O i Java som brukes i grublegruppe mandag 5. oktober 2015 (Slides går høyre, så helt ned, topp høyre og ned igjen)
- 698