Methods: String Next

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Scanner Class

The java.util.Scanner class is a simple text scanner which can parse primitive types and strings using regular
expressions.Following are the important points about Scanner:
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
A scanning operation may block waiting for input.

Methods
String next()
This method finds and returns the next complete token from this scanner.
String next(String pattern)
This method returns the next token if it matches the pattern constructed from the
specified string
boolean nextBoolean()
This method scans the next token of the input into a boolean value and returns that
value.
byte nextByte()
This method scans the next token of the input as a byte
float nextFloat()
This method scans the next token of the input as a float.
int nextInt()
This method scans the next token of the input as an int.
String nextLine()
This method advances this scanner past the current line and returns the input that was
skipped.
long nextLong()
This method scans the next token of the input as a long.
short nextShort()
This method scans the next token of the input as a short.

Example:
import java . util . Scanner ;
public class intro
{
public static void main ( String args []) {
String name ;
int age ;
Scanner s = new Scanner ( System . in );
System . out . println (" Enter your name ");
name = s . next ();
System . out . println (" Enter your age ");
age = s. nextInt ();
s.close();
System . out . println (" Name : " + name );
System . out . println (" Age : " + age );
}
}

1. Use the appropriate Scanner methods to read information about a student.


string-type name
short-type age
long-type ID
string-type date_of_birth
boolean-type variable to mention if the student is repeating the course or not.
Hint : to accept date of birth
s.next("\\d{2}-\\d{2}-\\d{4}");
Sample Result:
Enter your name: Ali
Enter your age: 22
Enter your ID Number: 20080034
Enter your date of birth:12-12-1999
Have you studied Java course previously (true/false)? false
Ali is 22 years old and has ID 20080034 Is he repeating the Java course? false
His date of birth is 12-12-1999.
Press any key to continue...
2. Write a java program to get the purchase details of a customer as input and generate the bill.Use
Array and loop.
Sample Result:
ABC Gift Mart
Bill No: 1056
Date: 18/11/2016
#
Item
Amount(Rs.)
1
2

Pen Stand
EcoPencil

UnitPrice(Rs.)
560
100

Qty
2
5

1120
500
Total

: Rs.

Received

: Rs.

1620
2000
Balance returned
Rs.

380

Thank You ! Visit us Again.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy