Scanner class for input Instantiate a new scanner
- Slides: 8
Scanner class for input • Instantiate a new scanner object Scanner in = new Scanner(System. in); • Getting input using scanner – int i = scanner. next. Int() – double d = scanner. next. Double(); – String s = scanner. next. Line(); • Differences from IO. read methods – There is no popup window – You just enter by typing at the bottom – You need to prompt the user what to enter Note: After next. Double() or next. Int(), scanner. next. Line() is required to consume the new line character
Command line Input • Using command line from Jgrasp – Click on build, then on run arguments – Enter the arguments that you want separated by spaces – Run the program and the program will use those arguments • Using the real command line – In Windows, click on start, then on run, then type cmd and click OK. You will be able to type Windows commands – Type: java <name of your program> <arguments separated with spaces> – Your program will run and use those command line arguments
Command Line Example 1. In Jgrasp, click on build, and then on run arguments, and enter 1 2 3 4 5 into the text box at the top 2. Enter the following program public class Command. Line { public static void main(String[] args) { int sum = 0; for (int i=0; i<args. length; i++) { sum += Integer. parse. Int(args[i]); } System. out. println("Sum = " + sum); } } 3. Compile and run, the output should be: sum = 15 Note: parse. Int is a static method that converts a string to an integer
Multiple Classes So far our applications had only a single class 1. Some applications can have hundreds of classes that work together to solve a problem 2. Each class is entered as a separate. java source file. 3. Compiling the main program will also compile all the classes it needs. • Design Challenge: How do we define an appropriate number of classes to create a structure for an application. This is a skill you learn in other Computer Science classes • Example: Creating a phone book (Following slides) – Class for a person – Class for a list of persons – Main class
The Person class public class Person { private String name, number; // Instance Variables public Person(String name, String number) // Constructor { this. name = name; this. number = number; } public String to. String() // Return nicely formatted string { return String. format("%-30. 30 s %-10. 10 s", name, number)); } } // End of Person class Note: private prevents outside class methods accessing the instance variables Note: The to. String() method is handy as you will see
Class for list of Persons public class Person. List { private Person[] persons; private int how. Many; public Person. List(int size) { persons=new Person[size]; how. Many=0; } } public boolean add. Person(String person, String number) { if (how. Many == persons. length) return false; else persons[how. Many++] = new Person(person, number); return true; } public String to. String() { String str = "Phone Book Listingn"; for (int i=0; i<how. Many; i++) str += persons[i]. to. String() + "n"; return str; }
Main class Input a group of persons, and then print out the list public class Main { private static Person. List person. List; public static void main(String[] args) { String phone, name; Scanner in = new Scanner(System. in); } } person. List = new Person. List(10); do { System. out. print("Enter name: " ); name = in. next. String(); System. out. print(“Enter number: ”); phone = in. next. String(); } while (person. List. add. Person(name, phone)); System. out. println(person. List);
Final Thoughts • Using multiple classes makes the application more general – The Person class can be used by other programs – The details of the Person class is internal. Users only have to know how to call its methods – We did this from day one without knowing it Examples: System. out. println(), Math. round(), and IO. read. String() • The private modifier is important – Normally make instance variables visible only where they are declared – This makes for easier modifications to an application – It enables the principle of limiting scope • The purpose of a constructor is to initialize an object • General Rule: Use the static modifier only when necessary