Scanner class for input - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Scanner class for input

Description:

In Windows, click on start, then on run, then type cmd and click OK. ... Compiling the main program will also compile all the classes it needs. ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 9
Provided by: set134
Category:
Tags: class | compile | input | scanner

less

Transcript and Presenter's Notes

Title: Scanner class for input


1
Scanner class for input
  • Instantiate a new scanner objectScanner 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

2
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 ltname of your programgt ltarguments
    separated with spacesgt
  • Your program will run and use those command line
    arguments

3
Command Line Example
  • In Jgrasp, click on build, and then on run
    arguments, and enter 1 2 3 4 5 into the text box
    at the top
  • Enter the following program
  • public class CommandLine
  • public static void main(String args)
    int sum 0 for (int i0
    iltargs.length i) sum
    Integer.parseInt(argsi)
    System.out.println("Sum " sum)
  • Compile and run, the output should be sum 15

Note parseInt is a static method that converts a
string to an integer
4
Multiple Classes
So far our applications had only a single class
  • Some applications can have hundreds of classes
    that work together to solve a problem
  • Each class is entered as a separate .java source
    file.
  • 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

5
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 toString() // Return nicely
    formatted string
  • String spaces
  • String s (name spaces).substring(0,spaces.le
    ngth())
  • String t (number spaces).substring(0,spaces.
    length())
  • return s t
  • // End of toString method
  • // End of Person class

Note We dont want outside class methods
accessing the instance variables, hence
private Note The toString() method is handy as
you will see Note The strings s and t mean that
all Person outputs will be formatted the same way
6
Class for list of Persons
  • public class PersonList
  • private Person persons
  • private int howMany
  • public PersonList(int size) personsnew
    Personsize howMany0
  • public boolean addPerson(String person, String
    number)
  • if (howMany persons.length) return false
  • else personshowMany new Person(person,
    number)
  • return true
  • public String toString()
  • String str Phone Book Listing\n
  • for (int i0 ilthowMany i) str
    personsi .toString() \n
  • return str

7
Main class
Input a group of persons, and then print out the
list
  • 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)

8
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
    itExamples 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
Write a Comment
User Comments (0)
About PowerShow.com