Scanner class |
|
/**The Scanner class extends from util class which extends from Object class. Scanner class allow us to read values from either the keyboard or file . Insted of the BufferReader class, with Scanner class, we do not need to convert the values from strings . */ import java.util.*; import java.io.*; class NumbersAndStrings { public static void main (String[] args) throws IOException { // I. BufferedReader: // I.1. Keyboard, InputStreamReader is used. System.out.print("\n\t Using BufferReader and ketboard:"); System.out.print("\n\t Enter a word: --> "); BufferedReader entranceW = new BufferedReader(new InputStreamReader(System.in)); String TheWord = entranceW.readLine(); System.out.println("\n\t The word entered is: " + TheWord + ". Its length is: " + TheWord.length()); System.out.print("\n\t Enter an integer: -->"); BufferedReader entranceN = new BufferedReader(new InputStreamReader(System.in)); int TheNumber = Integer.parseInt(entranceN.readLine()); // Converts string to integer. System.out.println("\n\t The integer entered is: " + TheNumber + ". Its square is: " + Math.pow(TheNumber,2)); System.out.println("\n"); //I.2.Input file: FileReader is used. System.out.print("\n\t Using BufferReader and files:"); final String ThisFile = "Words.dat"; BufferedReader Entrance = new BufferedReader(new FileReader (ThisFile)); boolean TheEndOfFile = false; while(!TheEndOfFile){ String TheLine = Entrance.readLine(); if (TheLine != null){ System.out.println("\n\t" + TheLine); // to list the words in the list } else TheEndOfFile = true; } Entrance.close(); // II. Scanner: ----------------------------------------------------------------- // II.1. Keyboard, InputStreamReader is not used. System.out.print("\n\t Using Scanner and Keyboard:"); Scanner in = new Scanner(System.in); int TheInteger; long TheLong; float TheFloat; double TheDouble; String TheString; String TheStringN; System.out.print("\n\tEnter an integer, a float, a string, and any other value; \n\t" + "separated by a whiteSpace (blank or return):\n\n\t --> "); TheInteger = in.nextInt(); //TheLong = in.nextLong(); TheFloat = in.nextFloat(); //TheString1 = in.next(); //TheDouble = in.nextDouble(); TheString = in.nextLine(); TheStringN = in.next(); //follows the previous and take its type System.out.println("\n\tHere is the output: "); System.out.println("\n\t" + TheInteger + " " + TheFloat + " " + TheString + " and " + TheStringN); //II.2.Input file: FileReader is always used. System.out.print("\n\t Using Scanner and files:"); Scanner EntranceS = new Scanner(new FileReader(ThisFile)); String ThisString; while (EntranceS.hasNext()){//method of Scanner class ThisString = EntranceS.next(); System.out.println("\n\t" + ThisString); } EntranceS.close(); System.out.println(); } } /* Summary I. BufferReader: I.1. Reading from files: final String ThisFile = "Words.dat"; BufferedReader Entrance = new BufferedReader(new FileReader (ThisFile)); String TheLine = Entrance.readLine(); System.out.prontln("TheLine"); Entrance.close(); I.2. Reading from Keyboard: InputStreamReade is used. System.out.print("\n\t Enter a word: --> "); BufferedReader entranceW = new BufferedReader(new InputStreamReader(System.in)); String TheWord = entranceW.readLine(); System.out.println("\n\tYou entered:" + TheWord); entranceW.close(); System.out.print("\n\t Enter an integer: -->"); BufferedReader entranceN = new BufferedReader(new InputStreamReader(System.in)); int TheNumber = Integer.parseInt(entranceN.readLine()); System.out.println("\n\tYou entered:" + TheNumber); entranceN.close(); II. Scanner: II.1. Reading from files: Scanner Entrance = new Scanner(new FileReader(ThisFile)); II.2. Reading from Keyboard: Reading from Keyboard: InputStreamReade is not used. Scanner TheInput = new Scanner(System.in); int TheInteger; System.out.print("\n\tEnter an integer:--> "); TheInteger = TheInput.nextInt(); System.out.println("\n\tYou entered: "+ TheInteger); The values taken by Scanner class are: SomeThing = in.nextInt(); = in.nextLong(); = in.nextFloat(); = in.next(); = in.nextDouble(); = in.nextLine(); = in.next(); //follows the previous and take its type Remarks: 1. To read from the keyboard, new InputStreamReader is not used as with Buffer Reader. 2. To read from a file, we instantiate a Scanner object with a FileReader object rather than System.in as in reading from the keyboard. 3. in.next is used for any following next. 4. The return carriage and blank space are considered as characters. In Java, they are called WhiteSpace. 5. When the declarations are not respected like : int TheInteger; TheInteger = in.next(); or TheInteger = in.nextLine(); an arror occurs. 6. When the type of the input is different from the declared type, an exception rises. The kind of exception is expressed as: Exception in thread "main" java.util.InputMismatchException */ |