1 / 14

Reading Information From the User

Reading Information From the User. Making your Programs Interactive. Classes used for Reading Information. Scanner (specific to JAVA 5 and beyond) Import java.util.*; BufferedReader (has always been there) Import java.io.*;. Scanner Class -The Methods. Constructor

kirima
Download Presentation

Reading Information From the User

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. Reading InformationFrom the User Making your Programs Interactive

  2. Classes used for Reading Information • Scanner (specific to JAVA 5 and beyond) • Import java.util.*; • BufferedReader (has always been there) • Import java.io.*;

  3. Scanner Class -The Methods • Constructor • Scanner sc = new Scanner(System.in); • Reading a String • String sc.nextLine( ); • Reading an int • int sc.nextInt( ); • Reading a double • double sc.nextDouble( );

  4. java.util.Scanner -the Steps • At the top must import java.util.Scanner; • Declare a Scanner object within the variable declaration area: • Scanner scan = new Scanner(System.in); Now you can use it to read information from the user System.in refers to The users’ keyboard input

  5. java.util.Scanner -more Steps • Declare a variable to store the input String sName; System.out.print(“What is your name? “ ); sName = scan.nextLine( ); System.out.println(“Nice to meet you “ + sName); System.in refers to The users’ keyboard input

  6. Reading integers with the Scanner class • Reading an int number int iAge = 0; Int iYear = 2006; System.out.print(“How old are you? “); iAge = scan.nextInt( ); iYear = iYear - iAge; System.out.println(“\nSo you were born around “ + iYear);

  7. Reading doubles with the Scanner Class • Reading a double number final double dGoal = 1000000; //constant double dEarnings = 0; double dMissing = 0; System.out.print(“How much do you make per hour? “); dEarnings = scan.nextDouble( ); dMissing = dGoal - dEarnings; System.out.println(“\nIn one hours you will only need “ + iMissing + “more to earn your goal”);

  8. Scanner Problems This class has problems when used several times in a row. Java 5 is still unstable See java/ReadInput/ReadWScanner.java

  9. java.io.BufferedReader • The BufferedReader class can only read Strings • However we can transform those Strings into numbers of any kind. • This one is always safe.

  10. Two Objects and one Exception • In the class: • public static void main(String args[ ]) throws IOException{ • InputStreamReader isr = new InputStreamReader (System.in); • BufferedReader br = new BufferedReader(isr); • The method: • String readLine( )

  11. Reading a String with BufferedReader InputStreamReader isr = new InputStreamReader (System.in); BufferedReader br = new BufferedReader(isr); String sName; System.out.print(“What is your name? “); sName = br.readLine( ); System.out.println(“So your name is “ + sName);

  12. Reading integers with BufferedReader String sPlaceHolder; int iAge =0; int iYear = 2006; System.out.print(“How old are you? “); sPlaceHolder = br.readLine( ); iAge = Integer.parseInt(sPlaceHolder); System.out.println(“So you are “ + iAge + “ years old”); System.out.println(“you were probably born in “+ (iYear - iage));

  13. Reading doubles with BufferedReader • Reading a double number final double dGoal = 1000000; //constant double dEarnings = 0; double dMissing = 0; System.out.print(“How much do you make per hour? “); sPlaceHolder = br.readLine( ) dEarnings = Double.parseDouble(sPlaceHolder); dMissing = dGoal - dEarnings; System.out.println(“\nIn one hours you will only need “ + iMissing + “more to earn your goal”);

  14. Questions? BufferedReader import java.io.*; readLine( ); Scanner import java.util.*; Java/ReadInput/ReadWBufReader.java Java/ReadInput/ReadWScanner.java

More Related