Computer Science 15-100 (APEA Section E), Summer 2008
Class Notes: Graphics Redux
Logistics
Topic Outline:
b) As Expressions
class MyCode {
public static void main(String[] args) {
int x, y;
x = 5;
y = x++;
System.out.println(x + "," + y); // ?,?
x = 5;
y = ++x;
System.out.println(x + "," + y); // ?,?
x = 5;
y = 10 + x--;
System.out.println(x + "," + y); // ?,?
x = 5;
y = 10 + --x;
System.out.println(x + "," + y); // ?,?
}
}a) From p. 87: Just need to know the
methods next***()
next(), nextLine(), nextBoolean(), nextByte(), etc...
b) next() versus nextLine()
Does not work quite as you might expect:
* next returns next input token as a String
* nextLine returns the rest of the current line as a String
* they do not mix-and-match well!
class MyCode {
public static java.util.Scanner scanner = new java.util.Scanner(System.in);
public static void main(String[] args) {
String s1, s2;
System.out.print("Enter a few words: ");
s1 = scanner.next();
System.out.print("Enter a few other words: ");
s2 = scanner.nextLine();
System.out.println("s1 = '" + s1 + "'");
System.out.println("s2 = '" + s2 + "'");
}
}
Do this again, but just use next, not nextLine:
class MyCode {
public static java.util.Scanner scanner = new java.util.Scanner(System.in);
public static void main(String[] args) {
String s1, s2, s3;
System.out.print("Enter a few words: ");
s1 = scanner.next();
s2 = scanner.next();
s3 = scanner.next();
System.out.println("s1 = '" + s1 + "'");
System.out.println("s2 = '" + s2 + "'");
System.out.println("s3 = '" + s3 + "'");
}
}
Once more, but just use nextLine, not next:
class MyCode {
public static java.util.Scanner scanner = new java.util.Scanner(System.in);
public static void main(String[] args) {
String s1, s2;
System.out.print("Enter a few words: ");
s1 = scanner.nextLine();
System.out.print("Enter a few other words: ");
s2 = scanner.nextLine();
System.out.println("s1 = '" + s1 + "'");
System.out.println("s2 = '" + s2 + "'");
}
}
Graphics (redux)
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem