3 Strings, Casting, Input

Oracle Academy Section

Section 3: Java Data Types

Lesson 3: Textual Data

Lesson 4: Converting Between Data Types

Lesson 5: Keyboard Input


Java Foundations Certification Exam Topics

Working with Java Data Types

  • Cast a value from one data type to another including automatic and manual promotion

  • Declare and initialize a String variable

Working with the String Class

  • Develop code that uses methods from the String class

Key Resources

  1. Java Tutorials:

    1. Strings

      1. Converting Between Numbers and Strings

      2. Manipulating Characters in a String

      3. Comparing Strings and Portions of Strings

      4. Escape Sequences

  2. geeksforgeeks

    1. Type conversion in Java with Examples

  3. Udemy

    1. Getting User Input (8:38) Scanner class and object, Ctrl Shift O, .nextLine(), making comments, .nextInt(), Ctrl Space autocomplete, exception when text entered for nextInt()

    2. Strings: Working with Text (9:21) primitive types, actually a class, +

    3. Casting Numerical Values (11:16)


Supplemental Resources

Most Important Concepts / Code

Strings are exact text and are in double quotes.

Strings are objects. The String class has many useful methods such as

  • length

  • valueOf

  • equals

  • substring

  • compareTo

  • toUpperCase

String Methods

String palindrome = "Dot saw I was Tod";

int len = palindrome.length();

String str = String.valueOf(num);

Strings are immutable.

// It might look the string changes but it does not. Really the object sample just points to a different string.

String sample = "Hello";

sample = "World";

// You could output the character at a specific location with something like System.out.println(sample.charAt(2));

// Because strings are immutable, you could not do something like sample.charAt(2) = 'g';

A String is a sequence of characters (like an array).

// This code loops through the characters.

String sample = "test";

char[] charArray = sample.toCharArray();

for(char c: charArray) {

System.out.println(c);

}

StringBuilder

// A StringBuilder is an alternative to a regular String that has more features and functionality.

StringBuilder sb = new StringBuilder();

sb.append("Greetings");

sb.append(" Friend");

System.out.println(sb);

Getting Input

System.out.println("Enter x1"); // prompt

int x1 = scanner.nextInt(); // get int input, will crash if input isn't an int

double rate = scanner.nextDouble(); // get double input, will crash if input isn't a number

// when going from reading numbers to reading strings you could have a problem because nextInt and nextDouble don't consume the Enter keystroke

scanner.nextLine(); // Consume left-over newline

String firstName = scanner.next(); // reads input until a space

String address = scanner.nextLine(); // reads input including spaces until the end of line \n

Casting - going from one data type to another

// dataType variableName = (dataType)variableToConvert;

double num = 2.2;

int num2 = (int)num;

Advanced Input:

// a safe way to get int input

int x1 = -999;

while (x1 == -999) {

System.out.println("Enter x1");

if (scanner.hasNextInt()) {

x1 = scanner.nextInt();

}

else {

System.out.println("Requires an integer");

scanner.next();

}

}

System.out.println("The number entered was " + x1);

Hour 1

Hour 2

  • Change String object after it is created.

  • Default String value.

  • Casting

    • widening - automatic

      • int num1 = 4;

      • double num2 = num1;

    • narrowing - explicit / manual

      • dataType variableName = (dataType)variableToConvert;

    • System.out.println(Integer.valueOf("66.5"));

  • Open project from GitHub Desktop