-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture19Strings.java
More file actions
33 lines (32 loc) · 879 Bytes
/
Lecture19Strings.java
File metadata and controls
33 lines (32 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Scanner;
/**
*
* Code examples from the Lecture slides
* Lecture 19
* Strings
*
* @author P.M.Campbell
* @version 2020-fall
*
*/
public class Lecture19Strings {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
String colour;
/*
hello
01234
length of 5
so last char is length -1
*/
System.out.println("What is your favourite colour? ");
colour = reader.next();
System.out.println("length "+ colour.length());
System.out.println("1st char " + colour.charAt(0));
System.out.println("last char " + colour.charAt(colour.length()-1));
// what happens here ?
//System.out.println("last char " + colour.charAt(colour.length()));
//System.out.println("last char " + colour.charAt(-5));
System.out.println("last char " + colour.charAt(9.5));
}
}