Documente Academic
Documente Profesional
Documente Cultură
Slides 8/29
Topics
• Strings
• Basic I/O
• Control Flow
• Arrays
String Basics
• Example program:
- Write info about the course to a file
Basic I/O- Scanners
• Useful methods:
- hasNext()- Tells whether there is an next input
token
- hasNextInt(), hasNextDouble(), etc.- Tells
whether the next token is an int, double, etc.
- next()- Gets the next input token (String)
- nextInt(), nextDouble(), etc.- Gets the next int,
double, etc.
- nextLine()- Gets the rest of line. Consumes the
line separator, but does not include it in the
returned value
Basic I/O- Scanners
• Example Programs
- Read an integer from the user and
print it to the screen
- Read every token from a file and print
it to the screen
Control Flow
switch(answer){
case ‘y’:
System.out.println(“Confirmed”);
break;
case ‘n’:
System.out.println(“Denied”);
break;
default:
System.out.println(“Not understood”);
}
Control Flow
while(n<128){
n *= 2;
}
Contol Flow
do{
System.out.println(“Enter a number”);
n = stdin.nextInt();
}while(n!=0);
Arrays