Computer Science  Regular Session  Summer Session  The Site
 CoursesContestsComputersLanguagesInternetActivitiesLibraryQuotations

String Class

Input - Reading from the Keyboard

 

String Input

Using Scanner Class

Example 1a

next () and nextLine ()

Example 1b

next () and nextLine ()

Example 2

Encrpytion Using ASCII Table

 

String Input

 

next Method

Will return a reference to a String object that contains from 0 to the number of characters entered at the keyboard.

It will stop when it reaches white space.

 

nextLine Method

Will return a reference to a String object that contains from 0 to the number of characters entered at the keyboard.

It will read white space and tabs but will end when it reaches a newline character.

 

Example 1

 

import java.util.Scanner;

public class Strings

{

      public static void main(String[] args)

      {

            Scanner keyboard = new Scanner(System.in);

            String entry1, entry2, entry3, entry4;

            System.out.println("Please enter a sentence");

            entry1 = keyboard.next();         //Reads to white space and stays on same line

            entry2 = keyboard.next();         //Reads to white space and stays on same line

            entry3 = keyboard.next();         //Reads to white space and stays on same line

            System.out.println("entry1 = " + entry1);

            System.out.println("entry2 = " + entry2);

            System.out.println("entry3 = " + entry3);

            System.out.println("Please enter another sentence");

            keyboard.nextLine();                     //moves to next line

            entry4 = keyboard.nextLine();   //reads entire line - to the newline character

            System.out.println("entry4 = " + entry4);

      }

}

 

This is the first sentence.

entry1 = This

entry2 = is

entry3 = the

Please enter another sentence

This is the second sentence.

entry4 = This is the second sentence.

 

Example 1b

 

import java.util.Scanner;

public class Strings

{

      public static void main(String[] args)

      {

            Scanner keyboard = new Scanner(System.in);

            String entry1, entry2, entry3, entry4;

            System.out.println("Please enter a sentence");

            entry1 = keyboard.next();           //Reads to white space and stays on same line

            entry2 = keyboard.next();           //Reads to white space and stays on same line

            entry3 = keyboard.next();           //Reads to white space and stays on same line

            System.out.println("entry1 = " + entry1);

            System.out.println("entry2 = " + entry2);

            System.out.println("entry3 = " + entry3);

            entry4 = keyboard.nextLine();   //Reads remainder of line to the new line character

            System.out.println("entry4 = " + entry4);

      }

}

 

Please enter a sentence

This is a     sentence.

entry1 = This

entry2 = is

entry3 = a

entry4 =      sentence.       //Note that the white space entered above was read.


Example 2

Encryption using ASCII Table

 

public class Strings

{

    public static void main(String[]args)

    {

        String string1 = "This is a string.";

        System.out.println("The contents of the string:");

       for (int i = 0; i < string1.length();i++)

        {

            System.out.print(string1.charAt(i));

        }

        System.out.println();

        System.out.println("The equivalent ASCII numbers for the string");

       for (int j = 0; j < string1.length();j++)

        {

            System.out.print((int)string1.charAt(j) + " ");

        }

        System.out.println();

        System.out.println("Adding 2 to each of the above numbers");

       for (int k = 0; k < string1.length();k++)

        {

            System.out.print((int)string1.charAt(k) + 2 + " ");

        }

        System.out.println();

        System.out.println("Converting back to characters");

       for (int m = 0; m < string1.length();m++)

        {

            System.out.print((char)((int)string1.charAt(m) + 2));

        }

    }

}


Output


The equivalent ASCII numbers for the string

84 104 105 115 32 105 115 32 97 32 115 116 114 105 110 103 46

Adding 2 to each of the above numbers

86 106 107 117 34 107 117 34 99 34 117 118 116 107 112 105 48

Converting back to characters

Vjku"ku"c"uvtkpi0