O to read multiple values on the same line we use

This preview shows page 40 - 44 out of 57 pages.

o To read multiple values on the same line, we use methods of the String class to separate the line into several strings held in an array (to be detailed later). n split() - split a string into an array of strings n trim() - trim trailing spaces of a string to avoid parsing errors o Integer.parseInt("10") works fine, but o Integer.parseInt(" 10") gets an error about number format o See example code on next page. 40 All of the Number classes - Integer, Float, Double, and so on - have parseXXX methods that convert a String representing a number to their primitive type.
Image of page 40

Subscribe to view the full document.

Method 1: Using the BufferedReader Class 41 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BufferedReaderDemo2 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter your name, age and height (comma-separated): "); String line = reader.readLine(); String[] data = line.split(","); String name = data[0].trim(); int age = Integer.parseInt(data[1].trim()); double height = Double.parseDouble(data[2].trim()); System.out.println("Your name is " + name + "."); System.out.println("Your age is " + age + "."); System.out.println("Your height is " + height + "cm."); } }
Image of page 41
42 Method 2: Using the Scanner Class o Java provides a utility class named “Scanner” under the package java.util for getting user input from keyboard. o Advantages: Convenient methods for parsing primitives from the tokenized input. o To use the Scanner class, you can follow the syntax shown below import java.util.Scanner; Syntax: where <object name> is name of the newly created Scanner class’s object, new is the keyword to create an object, <variable name> is the name of the variable to store the data obtained from the keyboard using Scanner method Scanner <object name> = new Scanner(System.in); <variable name> = <object name> . <method name> (); Place this line at the top of the Java source file Create a Scanner object Use the Scanner class’s methods to read input (Methods provided by the Scanner class are shown on the next page)
Image of page 42

Subscribe to view the full document.

43 Scanner Class Methods Method name Description boolean nextBoolean() Returns the next input token as a boolean value byte nextByte() Returns the next input token as a byte value short nextShort() Returns the next input token as a short value int nextInt() Returns the next input token as an int value long nextLong() Returns the next input token as a long value float nextFloat() Returns the next input token as a float value double nextDouble() Returns the next input token as a double value String next() Returns the next input token as a String value String nextLine() Returns all input remaining on the current line as a String value
Image of page 43
44 /* An example showing how to get input from keyboard Author: Desmond Tsoi */ import java.util.Scanner; public class InputDataDemo1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your name: "); String name = sc.nextLine(); System.out.print("Enter your age: "); int age = sc.nextInt(); System.out.print("Enter your gender: "); String gender = sc.next(); System.out.print("Enter your height: ");
Image of page 44
You've reached the end of this preview.
  • Fall '16
  • Dr Desmond Tsoi

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern