barcodefontsoft.com

Console I/O in Java Integrating pdf417 2d barcode in Java Console I/O visual .net QR Code

9.4 Console I/O using none toencode none on asp.net web,windows applicationc# qr code tutorial // Wrap th none for none e reader with a buffered reader. BufferedReader buf-in = new BufferedReader (reader); // Obtain a 16 bit stream. OutputStreamWriter writer = new OutputStreamWriter (System.

out); // Wrap the PrintWriter stream around the output stream PrintWriter print-writer = new PrintWriter (writer, true); String str = "q"; try { // Read a whole line a time. Check the string for // the "quit" input to jump from the loop. do { // Read text from keyboard str = buf-in.

readLine (); // PrintWriter output. print-writer.println ("echo " + str);.

Web application framework } while (! str.toLowerCase ().equals ("q")); } catch (IOException e) { System.

out.println ("IO exception = " + e); } } // main } // class BufferedReaderApp. A session none none with the program produces output like this:. My input f rom the keyboard echo My input from the keyboard More of my input echo More of my input q echo q. To obtain none for none numerical values from the keyboard requires reading them in as strings and then converting them to primitive types. For example, the following snippet will read in an integer value as a string and then convert it to an int value:. InputStrea mReader reader = new InputStreamReader (System.in); BufferedReader buf-reader = new BufferedReader (reader);. Java input/output try { Stri ng s = buf-reader.readLine (); // read the number as a // string // Trim the whitespace before parsing. tmp = Integer.

parseInt (s.trim ()); // Convert string to int System.out.

println (" echo = " + tmp);. } catch (I none none OException ioe) { System.out.println ("IO exception = " + ioe); }.

Similar co de is needed for parsing other numerical input according to the type of values. The following text discusses an easier technique for numerical input using the Scanner class of J2SE 5.0.

. 9.4.4 Text input with Scanner The java.u til.Scanner class is a very useful new tool that can parse text for primitive types and substrings using regular expressions [4,5].

It can get the text from various sources such as a String object, an InputStream, a File (Section 9.5), or a class that implements the Readable interface. The Scanner splits its input into substrings, or tokens, separated by delimiters, which by default are any white space.

The tokens can then be obtained as strings or as primitive types if that is what they represent. For example, the following code snippet shows how to read an integer from the keyboard:. Scanner sc none for none anner = new Scanner (System.in); int i = scanner.nextInt ();.

For each o f the primitive types there is a corresponding nextXxx() method that returns a value of that type. If the string cannot be interpreted as that type, then an InputMismatchException is thrown. There is also a set of hasNextXxx() methods, such as hasNextInt(), that return true or false according to whether the next token matches the speci ed type.

. import jav none for none a.io.*; import java.

util.*; /** Demonstrate the Scanner class for input of numbers.**/ public class ScanConsoleApp.

public sta tic void main (String arg[]) { // Create a scanner to read from keyboard Scanner scanner = new Scanner (System.in);. 9.5 The File class try { Syst em.out.printf("Input int (e.

g. %4d): ",3501); int int-val = scanner.nextInt (); System.

out.println ("You entered" + int-val +"\n"); System.out.

printf ("Input float (e.g. %5.

2f): ", 2.43); float float-val = scanner.nextFloat (); System.

out.println ("You entered" + float-val +"\n"); System.out.

printf ("Input double (e.g. %6.

3e): ", 4.943e15); double double-val = scanner.nextDouble (); System.

out.println ("You entered" + double-val +"\n");. } catch (I none for none nputMismatchException e) { System.out.println ("Mismatch exception:" + e); } } // main } // class ScanConsoleApp.

A session with the program ScanConsoleApp is shown here:. Input int none none (e.g. 3501): 23431 You entered 23431 Input float (e.

g. 2.43): 1.

2343 You entered 1.2343 Input double (e.g.

4.943e+15): -2.34e4 You entered -23400.

0. There are a number of other useful methods in the Scanner class such as skip() to skip over some input, useDelimiter() to set the delimiter, and findInLine() to search for substrings. The Scanner class uses tools from the java.util.

regex package for pattern matching with regular expressions. We don t have space here to describe these very powerful text-matching tools but you can nd more info in the Java 2 API Speci cations..

Copyright © barcodefontsoft.com . All rights reserved.