8
Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() – double d = scanner.nextDouble(); – String s = scanner.nextLine(); 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

Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –

  • View
    219

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –

Scanner class for input• Instantiate a new scanner object

Scanner in = new Scanner(System.in);• Getting input using scanner– int i = scanner.nextInt()– double d = scanner.nextDouble();– String s = scanner.nextLine();

• 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

Page 2: Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –

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

Page 3: Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –

Command Line Example1. In Jgrasp, click on build, and then on run arguments, and

enter 1 2 3 4 5 into the text box at the top2. Enter the following program

public class CommandLine{

public static void main(String[] args) { int sum = 0; for (int i=0; i<args.length; i++) { sum += Integer.parseInt(args[i]); } System.out.println("Sum = " + sum); }}

3. Compile and run, the output should be: sum = 15

Note: parseInt is a static method that converts a string to an integer

Page 4: Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –

Multiple Classes

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

So far our applications had only a single class

Page 5: Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –

The Person classpublic class Person{ private String name, number; // Instance Variables

public Person(String name, String number) // Constructor{ this.name = name;

this.number = number;}public String toString() // Return nicely formatted string{ String spaces = “ ”;

String s = (name + spaces).substring(0,spaces.length());String t = (number + spaces).substring(0,spaces.length());return s + “ ” + t;

} // End of toString method} // End of Person class

Note: We don’t want outside class methods accessing the instance variables, hence privateNote: The toString() method is handy as you will seeNote: The strings s and t mean that all Person outputs will be formatted the same way

Page 6: Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –

Class for list of Personspublic class PersonList{ private Person[] persons;

private int howMany;

public PersonList(int size) { persons=new Person[size]; howMany=0; }

public boolean addPerson(String person, String number){ if (howMany == persons.length) return false;

else persons[howMany++] = new Person(person, number);return true;

}public String toString() { String str = “Phone Book Listing\n”; for (int i=0; i<howMany; i++) str += persons[i] .toString() + “\n”;

return str;} }

Page 7: Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –

Main class

public class Main{ private static PersonList personList;

public static void main(String[] args){ String phone, name;

Scanner in = new Scanner(System.in);

personList = new PersonList(10);do{ System.out.print(“Enter name: ” ); name = in.nextString(); System.out.print(“Enter number: ”); phone = in.nextString();} while (personList.addPerson(name, phone));System.out.println(personList);

}}

Input a group of persons, and then print out the list

Page 8: Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –

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.readString()

• 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