1 / 14

Simple Input

Simple Input. Interactive Input - The Class Scanner Command-Line Arguments. Simple Input. Sometimes data is needed and obtained from the user at run time. Simple keyboard input requires: import java.util.Scanner; or import java.util.*; at the beginning of the file. Simple Input, cont.

yan
Download Presentation

Simple Input

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simple Input • Interactive Input - The Class Scanner • Command-Line Arguments

  2. Simple Input • Sometimes data is needed and obtained from the user at run time. • Simple keyboard input requires: import java.util.Scanner; or import java.util.*; at the beginning of the file.

  3. Simple Input, cont. • A “Scanner” object must be initialized before inputting data: Scanner keyboard = new Scanner(System.in); • To input data: int eggsPerBasket; : eggsPerBasket = keyboard.nextInt(); which reads one int value from the keyboard and assigns it to the variable eggsPerBasket.

  4. Simple Input, cont. import java.util.Scanner; public class Cows { publicstaticvoid main (String[] args) { int barns, cowsPer, totalCows; Scanner kb = new Scanner(System.in); System.out.print(“Enter the number of barns: ”); barns = kb.nextInt(); System.out.print(“Enter the number of cows per barn: ”); cowsPer = kb.nextInt(); totalCows = barns * cowsPer; System.out.println(“Total number of cows is: “ + totalCows); System.out.println(“Suppose two cows per barn escape!”); cowsPer = cowsPer – 2; totalCows = barns * cowsPer; System.out.println(“Total number of cows is now: “ + totalCows); } } > javac Cows.java > java Cows Enter the number of barns: 10 Enter the number of cows per barn: 6 Total number of cows is: 60 Suppose two cows per barn escape! Total number of cows is now: 40

  5. Command-Line Arguments • Frequently input is provided to a program at the command-line. public class UseArgument *Notice Scanner is not imported! { public static void main(String[] args) { System.out.print(“Hi, ”); System.out.print(args[0]); System.out.println(“. How are you?”); } } > javac UseArgument.java > java UseArgument Alice Hi, Alice. How are you? > java UseArgument Bob Hi, Bob. How are you?

  6. Command-Line Arguments • Frequently input is provided to a program at the command-line. public class UseArgument { public static void main(String[] args) { System.out.print(“Hi, ”); System.out.print(args[0]); System.out.println(“. How are you?”); } } > javac UseArgument.java > java UseArgument Alice Hi, Alice. How are you? > java UseArgument Bob Hi, Bob. How are you?

  7. Command-Line Arguments • Frequently input is provided to a program at the command-line. public class UseArgument { public static void main(String[] args) { System.out.print(“Hi, ”); System.out.print(args[0]); System.out.println(“. How are you?”); } } > javac UseArgument.java > java UseArgument Alice Hi, Alice. How are you? > java UseArgument Bob Hi, Bob. How are you?

  8. Command-Line Arguments • Frequently multiple values are provided at the command-line. public class Use3Arguments { public static void main(String[] args) { System.out.print(“The first word is ”); System.out.print(args[0]); System.out.print(“, the second is ”); System.out.print(args[1]); System.out.print(“, and the third is ”); System.out.println(args[2]); } } > javac Use3Arguments.java > java Use3Arguments dog cat cow

  9. Command-Line Arguments • Frequently multiple values are provided at the command-line. public class Use3Arguments { public static void main(String[] args) { System.out.print(“The first word is ”); System.out.print(args[0]); System.out.print(“, the second is ”); System.out.print(args[1]); System.out.print(“, and the third is ”); System.out.println(args[2]); } } > javac Use3Arguments.java > java Use3Arguments dog cat cow

  10. Command-Line Arguments • Frequently multiple values are provided at the command-line. public class Use3Arguments { public static void main(String[] args) { System.out.print(“The first word is ”); System.out.print(args[0]); System.out.print(“, the second is ”); System.out.print(args[1]); System.out.print(“, and the third is ”); System.out.println(args[2]); } } > javac Use3Arguments.java > java Use3Arguments dog cat cow The first word is dog, the second is cat, and the third is cow

  11. Command-Line Arguments • What if you provide integers on the command-line? public class IntOps { public static void main(String[] args) { int sum = args[0] + args[1]; int prod = args[0] * args[1]; int quot = args[0] / args[1]; int rem = args[0] % args[1]; System.out.println(“Sum = " + sum); System.out.println(“Product = " + prod); System.out.println(“Quotient = " + quot); System.out.println(“Remainder = " + rem); } } Hoping to see this > javac IntOps.java > java IntOps 1234 99 Sum = 1333 Prod = 122166 Quotient = 12 Remainder = 46

  12. Command-Line Arguments • What if you provide integers on the command-line? public class IntOps { public static void main(String[] args) { int sum = args[0] + args[1]; int prod = args[0] * args[1]; int quot = args[0] / args[1]; int rem = args[0] % args[1]; System.out.println(“Sum = " + sum); System.out.println(“Product = " + prod); System.out.println(“Quotient = " + quot); System.out.println(“Remainder = " + rem); } } > javac IntOps.java *** ERROR ***

  13. Command-Line Arguments • For the same reason, this does NOT work: public class IntOps { public static void main(String[] args) { int a = args[0]; int b = args[1]; int sum = a + b; int prod = a * b; int quot = a / b; int rem = a % b; System.out.println(“Sum = " + sum); System.out.println(“Product = " + prod); System.out.println(“Quotient = " + quot); System.out.println(“Remainder = " + rem); } } > javac IntOps.java *** ERROR ***

  14. Command-Line Arguments • So, it looks ugly, but this is what we need to do: public class IntOps { public static void main(String[] args) { int a = Integer.parseInt(args[0]); // Notice the variable declaration int b = Integer.parseInt(args[1]); // Notice the Integer.parseInt int sum = a + b; // Notice the comments…lol int prod = a * b; int quot = a / b; int rem = a % b; System.out.println(“Sum = " + sum); System.out.println(“Product = " + prod); System.out.println(“Quotient = " + quot); System.out.println(“Remainder = " + rem); } } > javac IntOps.java > java IntOps 1234 99 Sum = 1333 Prod = 122166 Quotient = 12 Remainder = 46

More Related