To gain experience with:
There are various ways to read input from the keyboard, the java.util.Scanner class is one of them.
The Java Scanner class breaks the input into tokens using a delimiter that is whitespace bydefault. It provides many methods to read and parse various primitive values.
Java Scanner class is widely used to parse text for string and primitive types using regular expression.
There is a list of commonly used Scanner class methods:
Method | Description |
---|---|
public String next() | it returns the next token from the scanner. |
public String nextLine() | it moves the scanner position to the next line and returns the value as a string. |
public byte nextByte() | it scans the next token as a byte. |
public short nextShort() | it scans the next token as a short value. |
public int nextInt() | it scans the next token as an int value. |
public long nextLong() | it scans the next token as a long value. |
public float nextFloat() | it scans the next token as a float value. |
public double nextDouble() | it scans the next token as a double value. |
//ReadingInput.java import java.util.Scanner; import java.io.IOException; public class ReadingInput { public static void main(String[] args)throws IOException { Scanner input = new Scanner(System.in); System.out.print("Please enter your name: "); String name = input.nextLine(); System.out.print("Now enter your age: "); int age = input.nextInt(); System.out.print("Enter amount to donate: "); double amount = input.nextDouble(); System.out.printf("Mr. %s after one year you will be %d years\n",name, (age+1)); System.out.printf("This is the amount you donated $%,.2f",amount); } }
1.
Write a Java program
that prompts for and reads the length, width, and height (in centimeters) of
a closed box. The program should then
compute and display (a)
the volume of the box. (b)
the surface area of the box. |
2.
Write a program that
prompts for and read e-mail address a user.
The program them prints the user name and the domain name on different
lines.(Hint: use indexOf() and substring() method) |
2.
Write a program that prompts user to read the sides and compute the area, perimter, longest, and smallest side of triangle. Compute and display the remainder when the longest side is divided by the smallest side. Print and format the output as |