Strings and text i o
This presentation is the property of its rightful owner.
Sponsored Links
1 / 18

Strings and Text I/O PowerPoint PPT Presentation


  • 45 Views
  • Uploaded on
  • Presentation posted in: General

Strings and Text I/O. Splitting a string. The split method can be used to extract tokens from a string with the specified delimiters. String s = "Michael David Harry Mark"; String[] names = s.split (" "); String s = "Michael David;Harry,Mark "; String[] names = s.split (“[ ,;]");

Download Presentation

Strings and Text I/O

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Strings and text i o

Strings and Text I/O


Splitting a string

Splitting a string

  • The split method can be used to extract tokens from a string with the specified delimiters.

  • String s = "Michael David Harry Mark";

  • String[] names = s.split(" ");

  • String s = "Michael David;Harry,Mark";

  • String[] names = s.split(“[ ,;]");

  • String[] names = s.split(“ ,;"); //totally different!!


Finding a character or a substring in a string

Finding a character or a substring in a string

  • String s = "Welcome to Java“;

  • s.indexOf('W') returns 0.

  • s.indexOf('x') returns -1.

  • s.indexOf("come") returns 3.

  • s.indexOf("Java") returns 11.

  • s.indexOf("java") returns -1.

  • s.indexOf(‘o’) returns 4.

  • s.indexOf(‘o’, 5) returns 9.


Converting strings to numeric values

Converting strings to numeric values

  • String s = "5";

  • inta = Integer.parseInt(s);

  • String s = "5";

  • double b = Double.parseDouble(s);


The character class

The Character class

  • Java provides a wrapper class for every primitive data type.

    • Character, Boolean, Byte, Short, Integer, Long, Float, Double

    • They enable the primitive data values to be treated as objects.

    • Character c = new Character(‘a’);

    • char c = ‘a’;


Comparing two characters

Comparing two characters

  • char c1 = ‘a’;

  • char c2 = ‘a’;

  • if (c1 == c2) // to compare primitive data

  • Character c1 = new Character('a');

  • Character c2 = new Character('a');

  • if (c1.equals(c2)) // to compare Character objects


Methods in the character class

Methods in the Character class

  • Most of the methods in the Character class are static methods.

  • Character.isDigit(char c): boolean

  • Character.isLetter(char c): boolean

  • Character.isLetterOrDigit(char c): boolean

  • Character.toLowerCase(char c): char

  • Character.toUpperCase(char c): char


Exercise 4 checking password

Exercise 4: Checking password

  • Write a method that checks whether a string is a valid password.

    • A password mush have at least eight characters.

    • A password consists of only letters and digits.

    • A password must contain at least two digits.

  • public static booleancheckPassword(String s)


The stringbuilder stringbuffer class

The StringBuilder/StringBuffer Class

  • an alternative to the String class

  • more flexible than String

    • You can add, insert, or append new contents into a string builder.

    • A String object is fixed.


Stringbuilder constructors

StringBuilder Constructors

  • String s = new String(“Welcome”);

  • StringBuilder s = new StringBuilder();

  • StringBuilder s = new StringBuilder(“Welcome”);


Modifying stringbuilder

Modifying StringBuilder

  • StringBuilders = new StringBuilder(“Welcome”);

  • s.append(" ");

  • s.append("to");

  • s.append(" ");

  • s.append("Java!");

  • s.insert(11, "HTML and ");

  • s.delete(11,20); //delete from index 11 to index 20-1

  • s.deleteCharAt(11);

  • s.reverse();


Other methods

Other methods

  • s.length()

  • s.charAt(int index)

  • s.toString()


Exercise 5 checking palindrome

Exercise 5: Checking Palindrome

  • A string is a palindrome if it reads the same forward and backward.

    • mom, dad, noon, 12321

  • Write a program that prompts the user to enter a string and reports whether it is a palindrome.

  • public static booleanisPalindrome(String s)


Command l ine arguments

Command-Line Arguments

  • public static void main (String[] args)

  • What is that args??

  • Example: passing two strings to the main method

  • java TestString hello class

  • java TestString 50 60


The file class

The File class

  • File name: test.txt

  • Absolute file name: C:\temp\test.txt

  • import java.io.File;

  • File f = new File(“test.txt”);

  • File f = new File(“C:\\temp\\test.txt”);

  • Backslash in Java is \\ (escape sequence).


The file class1

The File class

  • The File class contains the methods for obtaining file properties and for renaming and deleting files.

  • f.exists(): boolean

  • f.getName(): String

  • f.getAbsolutePath (): String

  • f.delete(): boolean

  • The File class does NOT contain the methods for reading and writing file contents.


File input and output

File Input and Output

  • Java I/O classes: Scanner and PrintWriter

  • import java.io.PrintWriter; (or java.io.*)

  • File f = new File("scores.txt");

  • PrintWriter pw = new PrintWriter(f);

  • creates a new file if the file does not exist

  • discards the current content in the file if the file already exists


The printwriter class

The PrintWriter class

  • pw.print(s: String): void

  • pw.println(s: String): void

  • //System.outis a standard Java object for the console.

  • pw.close(); //must be used to close the file. Otherwise the data may not be saved properly.


  • Login