package inputoutputexample; import java.util.Scanner; // examples of input output public class InputOutputExample { public static void formatNums() { int i1 = 10; int i2 = -20; double d1 = 5.50; double d2 = -7.700; String s1 = "a"; String s2 = "The name of the book about which this book report is about is Peter Rabbit."; // println prints whatever string it is given // + happens first to concatenate, which implicitly // casts ints and doubles to String System.out.println(i1 + " " + d1 + " " + s1); // 10 5.5 a // printf uses the formatting string // to determine how to print the other arguments // %d is placeholder for int // %f is placeholder for double or float // %s is placeholder for string // %n is newline System.out.printf("%d %f %s%n", i1, d1, s1); // 10 5.500000 a // extra arguments ignored System.out.printf("%d %f %s%n", i1, d1, s1, s2); // 10 5.500000 a // order of arguments matters System.out.printf("%d %d %f %f%n", i1, i2, d2, d1); // 10 -20 -7.700000 5.500000 // printf can control how many decimal places .n means n decimal places System.out.printf("%.1f %.10f%n", d2, d1); // -7.7 5.5000000000 //String.format uses the same formatting codes as printf // and returns a String System.out.println("money with two decimal places: $"+String.format("%.2f", d1)); } public static void getInput() { Scanner scan = new Scanner(System.in); // nextInt, nextDouble, and next // just get data up to next space or new line // so if you enter multiple things on same line // the following line will get it // if you enter // 1 2.3 a b c d e // x will be 1 // d will be 2.3 // s will be a // ss will be "b c d e" System.out.println("Enter Int"); int x = scan.nextInt(); System.out.println("You said " + x); System.out.println("Enter Double"); double d = scan.nextDouble(); System.out.println("You said " + d); System.out.println("Enter String"); String s = scan.next(); System.out.println("You said " + s); System.out.println("Enter String"); String ss = scan.nextLine(); System.out.println("You said " + ss); // if you add a call to nextLine // it will use up everything up to the next newline // anything there is thrown away System.out.println("Enter Int"); x = scan.nextInt(); scan.nextLine(); System.out.println("You said " + x); System.out.println("Enter Double"); d = scan.nextDouble(); scan.nextLine(); System.out.println("You said " + d); System.out.println("Enter String"); s = scan.next(); scan.nextLine(); System.out.println("You said " + s); // if we want the whole String to the end of the line // eg if we want to include spaces System.out.println("Enter String"); ss = scan.nextLine(); System.out.println("You said " + ss); // Scanner has methods that detect whether there // is a token of the right type // hasNextInt // hasNextDouble // hasNext -- treats anything as a string System.out.println("Enter an Int or a String to quit"); while (scan.hasNextInt()){ x = scan.nextInt(); System.out.println("You said " + x); System.out.println("Enter an Int or a String to quit"); } } public static void main(String[] args) { formatNums(); getInput(); } }