Mixing other data types with String data

  • Java allows values from other type of values to be concatenated to a String

  • The meaning of the + operation involving a String:

       String + (any) value  ≡  String + String repr of (any) value
    
       (any) value + StringString repr of (any) value + String 
    

    Examples:

        "Hello" + 3.14     ≡  "Hello" + "3.14"    // Result: "Hello3.14"
        "Hello" + 999      ≡  "Hello" + "999"     // Result: "Hello999"
        "Hello" + 'A'      ≡  "Hello" + "A"       // Result: "HelloA"
        "Hello" + (3 < 4)  ≡  "Hello" + "true"    // Result: "Hellotrue"
      
        777 + "Hello"      ≡   "777" + "Hello"    // Result: "777Hello"
      

DEMO: demo/04-Math+String/06-mix-with-string/Demo1.java

Quiz: What is assigned in the variable s ?

What is assigned to the variable s in each of the following statements:

public class MyProg 
{
   public static void main(String[] args) 
   {
     String s;

     s = "A" + 1;
     s = "1" + 1;
     s = "1" + 1 + 1;   // Careful: + is left associative !

     s = 1 + "1";
     s = 1 + 1 + "1";   // Careful: + is left associative !
   }
}


Note:   the meaning of the the + operation depends on the type of value in the operation !

Quiz: What is assigned in the variable s ?

Answers:

public class MyProg 
{
   public static void main(String[] args) 
   {
     String s;

     s = "A" + 1;       // "A1"
     s = "1" + 1;       // "11"
     s = "1" + 1 + 1;   // ("1" + 1) + 1 = "11" + 1 = "111"

     s = 1 + "1";       // "11"
     s = 1 + 1 + "1";   // (1 + 1) + "1" = 2 + "1" = 21
   }
}


DEMO: demo/04-Math+String/06-mix-with-string/Quiz.java

Usage of the + operation: an easier way to print messages with variables

  • Instead of using multiple print statements:

        double radius = 20, area;
      
        area = 3.14159 * radius * radius;
      
        System.out.print("Area of circle with radius ");
        System.out.print(radius);
        System.out.print(" is ");
        System.out.println(area);

    We can use 1 print statement with concatenation:

        double radius = 20, area;
      
        area = 3.14159 * radius * radius;
      
        System.out.println("Area of circle with radius " 
                            + radius + " is " + area);

DEMO: demo/04-Math+String/06-mix-with-string/Printing.java

Reading in words from keyboard

  • Word:

    • Word = a string (word) that is deliminated by one or more whitespaces (= space, TAB or new line)

  • Reading in a word from the keyboard in Java:

    • The Scanner class provides the next() method that can read in a word

  • Example: how to use next() to read in a word

     Scanner input = new Scanner(System.in);
          
     System.out.print("Enter three words separated by spaces: ");
     String s1 = input.next();
     String s2 = input.next();
     String s3 = input.next(); 

DEMO: demo/04-Math+String/06-mix-with-string/Next.java

Reading in lines from keyboard

  • Line:

    • Line = a string that is terminated by a new line
      A line can contain spaces and TABs

  • Reading in a line from the keyboard in Java:

    • The Scanner class provides the nextLine() method that can read in a line

  • Example: how to use nextLine() to read in a line

     Scanner input = new Scanner(System.in);
          
     System.out.print("Enter a line: ");
     String s1 = input.nextLine();

DEMO: demo/04-Math+String/06-mix-with-string/NextLine.java

Reading in a character from keyboard

  • Important fact:

    • Java does not have a method that reads in one character from the keyboard

  • In order to read in one character from keyboard:

    • Use the next() method to read in a word

    • Then use charAt(0) to extract the first character from the input string

  • Example:

      Scanner input = new Scanner(System.in);
      
      String  s1;
      char    c;         // Read in 1 character
    
      System.out.print("Enter a character: ");
      s1 = input.next(); // (1) read in a word
      c  = s1.charAt(0); // (2) extract character 
    

DEMO: demo/04-Math+String/06-mix-with-string/ReadChar.java