0% found this document useful (0 votes)
3 views11 pages

Java Random Part 3 Important

The document provides examples of using the Character class in Java to analyze characters in a string, including identifying letters, digits, and calculating the sum of numeric values. It also discusses the Scanner class for parsing input streams, demonstrating how to read from the console and files, and methods for retrieving tokens. Overall, it illustrates practical applications of these classes in Java programming.

Uploaded by

carawa6255
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Java Random Part 3 Important

The document provides examples of using the Character class in Java to analyze characters in a string, including identifying letters, digits, and calculating the sum of numeric values. It also discusses the Scanner class for parsing input streams, demonstrating how to read from the console and files, and methods for retrieving tokens. Overall, it illustrates practical applications of these classes in Java programming.

Uploaded by

carawa6255
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Character class

Example 1
public class CharacterTypes {
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.print("Enter a line: ");
String line = kb.nextLine();
// characters are examined one-by-one
for (int i = 0; i < line.length(); i++){
char c = line.charAt(i);
if( Character.isLetter(c) )
System.out.println(i+"\t"+c+"\t\tletter");
else if( Character.isDigit(c) )
System.out.println(i+"\t"+c+"\t\tdigit");
else
System.out.println(i+"\t"+c+"\t\tother");
}
}
}
11
Character class
Example 2
public class SumNumericValues

A line of text is examined, character-by-character, and the sum of the


numeric characters is calculated

Character methods used:


getNumericValue(…) returns the int value the character represents
isDigit(…) returns true if the character is a digit

No instance of Character is used which means the methods are called


using statements of the form
sum += Character.getNumericValue(c) ;

The argument passed to getNumericValue is c


The Character class
The method to execute is getNumericValue
12
Character class
Example 2
public class SumNumericValues
{
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.print("\nEnter a line: ");
String line = kb.nextLine();
int sum = 0;
// characters are examined one-by-one
for (int i = 0; i < line.length(); i++){
char c = line.charAt(i);
if( Character.isDigit(c) ){
sum += Character.getNumericValue(c);
}
}
System.out.println("sum = \t"+sum);
}
}

13
Character class
Example 3
public class ValidateStudentNumber

A student number, stored as a character string, is examined character-


by-character to determine if all characters are digits.

Character methods used:


isDigit(…) returns true if the character is a digit

No instance of Character is used which means the methods are called using
statements of the form
If ( ! Character.isDigit(c) ) valid = false ;

The argument passed to isDigit is c


The Character class
The method to execute is isDigit

14
Character class
Example 3
public class ValidateStudentNumber
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.println("Enter a number: ");
String number = kb.next();
// characters are examined one-by-one
boolean valid = true;
for (int i = 0; i < number.length(); i++){
char c = number.charAt(i);
if(! Character.isDigit(c) ) valid = false;
}
if (valid) System.out.println("Valid");
else System.out.println("Invalid");
}
}

15
Scanner class

The Scanner class is useful when you need to parse some stream that
is said to contain tokens.

For ACS-1903 we are concerned with tokens that comprise a


sequence of characters delimited by whitespace (space, tab, line feed
characters)

We can easily create a Scanner object that is associated with one of:
– System.in
– a string Streams containing tokens

– a file

Examples
Scanner s = new Scanner(System.in);
Scanner s = new Scanner(s); //s is of type String
Scanner s = new Scanner(f); //f is of type File
16
Scanner class

Scanner methods

next() get next token


nextBoolean() get next …
nextInt() get next …
nextDouble() get next …
nextLine() get next …
hasNext() returns true if there is a token available
hasNextLine() returns true if …
hasNextBoolean() returns true if …
hasNextInt() returns true if …
hasNextDouble() returns true if …

17
Scanner class
Examples

1. Read a file named Readme.txt … this file is in every BlueJ project.


Three import statements
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException
Exceptions are a topic in ACS-1904.
There is a throws clause – something can go wrong when reading and writing
files – a topic in ACS-1904

2. Scanning a string for tokens.


One import statement
import java.util.Scanner;

18
Scanner class

Example 1
public class DisplayReadme

The file Readme.txt is read, line-by-line, until there are no lines left.

Scanner methods used:


f.hasNext() returns true if there is another token in the stream f
f.nextLine()returns the next line in f

A Scanner object is needed to provide a reference to the file and the current
location in the file. Consider the statement:
Scanner f = new Scanner( new File("Readme.txt"));

The Scanner object is f The file Readme.txt

Create a Scanner object for a file

19
Scanner class

Example 1

public class DisplayReadme


{
public static void main(String[] args)
throws FileNotFoundException
{
Scanner f = new Scanner(new File("Readme.txt"));
int i=1;
System.out.println("<<<< File Readme.txt >>>>");
while ( f.hasNext() ){
String line = f.nextLine();
System.out.println((i++)+" "+line);
}
System.out.println("<<<< end of listing >>>>");
}
}

20
Scanner class

Example 2
public class ScanString

A string with whitespace separating the tokens is parsed


– each token is displayed on a separate line.

Scanner methods used:


s.hasNext() returns true if there is another token in the stream s
s.next() returns the next token in s

A Scanner object is needed that provides a reference to the file and the current
location in the file. Consider the statement:
String token = s.next() ;

The next token is assigned The method next()


to a variable of type String
The Scanner object named s

21

You might also like