Skip to content

Commit 40296f6

Browse files
committed
Fix style, close input to prevent resource leaks
1 parent 5dc7a93 commit 40296f6

File tree

30 files changed

+58
-10
lines changed

30 files changed

+58
-10
lines changed

src/com/codefortomorrow/beginner/chapter5/examples/DifferentInput.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ public static void main(String[] args) {
1818
System.out.println("Name: " + name);
1919
System.out.println("Pi: " + pi);
2020
System.out.println("Favorite integer: " + favoriteInteger);
21+
22+
reader.close();
2123
}
2224
}

src/com/codefortomorrow/beginner/chapter5/examples/FullName.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ public static void main(String[] args) {
2121

2222
// print the user's full name
2323
System.out.println("Your full name is " + firstName + " " + lastName);
24+
25+
input.close();
2426
}
2527
}

src/com/codefortomorrow/beginner/chapter5/solutions/Average.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@ public static void main(String[] args) {
2929
// calculate and display the average
3030
double average = (number1 + number2 + number3) / 3;
3131
System.out.println("The average is " + average);
32+
33+
input.close();
3234
}
3335
}

src/com/codefortomorrow/beginner/chapter5/solutions/CircleArea.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ public static void main(String[] args) {
2121
// calculate and display the circle's area
2222
double area = Math.PI * radius * radius;
2323
System.out.println("Area of the circle is: " + area);
24+
25+
input.close();
2426
}
2527
}

src/com/codefortomorrow/beginner/chapter6/examples/Vote.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public static void main(String[] args) {
88
Scanner input = new Scanner(System.in);
99
System.out.print("Enter your age: ");
1010
int age = input.nextInt();
11+
input.close();
1112

1213
// you can vote if you're 18+
1314
boolean canVote = age >= 18;

src/com/codefortomorrow/beginner/chapter6/solutions/Billing.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public static void main(String[] args) {
4747
System.out.print("Enter the price of the item: $");
4848
double price = input.nextDouble();
4949

50+
input.close();
51+
5052
// apply a discount if needed
5153
double discount = 0;
5254
if (price > 10) {

src/com/codefortomorrow/beginner/chapter6/solutions/Drive.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static void main(String[] args) {
2424
Scanner input = new Scanner(System.in);
2525
System.out.print("Enter your age: ");
2626
int age = input.nextInt();
27+
input.close();
2728

2829
// boolean expressions
2930
boolean isMinor = age < 18;

src/com/codefortomorrow/beginner/chapter6/solutions/EvenOrOdd.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static void main(String[] args) {
1212
Scanner input = new Scanner(System.in);
1313
System.out.print("Enter an integer: ");
1414
int number = input.nextInt();
15+
input.close();
1516

1617
if (number % 2 == 0) {
1718
System.out.println(number + " is even");

src/com/codefortomorrow/beginner/chapter6/solutions/Grade.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static void main(String[] args) {
1616
Scanner input = new Scanner(System.in);
1717
System.out.print("Enter a score (as a percent): ");
1818
double score = input.nextDouble();
19+
input.close();
1920

2021
// print the letter grade based on the score
2122
System.out.print("Grade: ");

src/com/codefortomorrow/beginner/chapter6/solutions/PickYourPath.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
public class PickYourPath {
1616
public static void main(String[] args) {
17+
final String PROMPT = "Enter 1, 2, or another number to quit: ";
18+
1719
System.out.println("Welcome to Pick Your Path!");
1820

1921
System.out.println("You are a master chef cooking in your restaurant\n" +
@@ -22,7 +24,7 @@ public static void main(String[] args) {
2224
System.out.println("\t(2) Call 911 and let the firefighters handle it");
2325

2426
Scanner input = new Scanner(System.in);
25-
System.out.print("Enter 1, 2, or another number to quit: ");
27+
System.out.print(PROMPT);
2628
int choice = input.nextInt();
2729

2830
if (choice == 1) {
@@ -42,7 +44,7 @@ public static void main(String[] args) {
4244
"\tassure the customers that their food is coming");
4345
System.out.println("\t(2) Stay in the kitchen and work faster");
4446

45-
System.out.print("Enter 1, 2, or another number to quit: ");
47+
System.out.print(PROMPT);
4648
choice = input.nextInt();
4749

4850
if (choice == 1) {
@@ -53,7 +55,7 @@ public static void main(String[] args) {
5355
System.out.println("\t(1) Invest in a new oven");
5456
System.out.println("\t(2) Book a vacation to Tahiti");
5557

56-
System.out.print("Enter 1, 2, or another number to quit: ");
58+
System.out.print(PROMPT);
5759
choice = input.nextInt();
5860

5961
if (choice == 1) {
@@ -77,5 +79,7 @@ public static void main(String[] args) {
7779
} else {
7880
System.out.println("Game over. Thanks for playing!");
7981
}
82+
83+
input.close();
8084
}
8185
}

0 commit comments

Comments
 (0)