[Java beginner] Resource leak <~~> will never be closed

Introduction

I am a beginner about a month after I started studying. I am writing within the knowledge I know, so I would be grateful if you could tell me any other ideas. It may also contain incorrect content.

table of contents

  1. What was made
  2. Thoughts during creation
  3. Problems and solutions that occurred during creation
  4. Issues and improvements
  5. Comments, what you would like to know
  6. Conclusion

This issue

0412java問題.png From Kitasoft Kobo

What was made

I thought that I could do it by using a switch statement, and it was completed in about 40 minutes.

//Kitasoft Kobo Java Exercises
import java.util.Scanner;

public class Example {
	public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int week; //Day of the week
	int time; //Time zone
	int result = 2; //0: Closed 1: Open 2:Update with error switch
	String[] weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
	String[] times = {"a.m.", "afternoon", "Night"};
	String[] results = {"It is a closed day", "it is open","error:Invalid input"};
	
	System.out.println("Please enter the number corresponding to the desired day and time");

	
		do {
			System.out.println("Day of the week: 0=Sunday 1=Monday 2=Tuesday 3=Wednesday 4=Thursday 5=Friday 6=Saturday");
			System.out.print("Day of the week:");
			week = sc.nextInt();
			if(week > 6) {
				System.out.println("Error: Invalid input");
				System.out.println("=====================");
			}
		} while(week > 6);
		
		do {
			System.out.println("Time: 0=1 am=2 pm=Night");
			System.out.print("Time zone:");
			time = sc.nextInt();
			if(time > 2) {
				System.out.println("Error: Invalid input");
				System.out.println("=====================");
			}
		} while(time > 2);
		
		sc.close();
		
		switch(week) {
		case 0:
			result = 1; //All closed
			break;
		case 1:
		case 4:
			result = 0; //All open
			break;
		case 2:
		case 5:
			if(time == 0) { //Closed in the morning
				result = 0;
				break;
			} else {
				result = 1;
				break;
			}
		case 3:
			if(time == 2) { //Closed at night
				result = 0;
				break;
			} else {
				result = 1;
				break;
			}
		case 6:
			if(time == 0) { //Open in the morning
				result = 1;
				break;
			} else {
				result = 0;
				break;
			}
			
		}
		
		System.out.println(weeks[week] + "of" + times[time] + "The hospital" + results[result]);
		
	}
	
}

What I thought while creating

I wanted to write it out for the time being, so I stopped using methods. Enter the day of the week and the time zone, and if you enter a different value, enter it again. ** → How can I start over? ** ** → Repeat with a do while statement

It is troublesome to write a lot when using a switch statement. ** → Is it possible to judge the day of the week and the time zone by logical product? ** ** → Give up without thinking of a way

Problems and solutions that occurred during creation

**-The local variable result may not have been initialized ** Initially, the value of the result field was not initialized.

int result; //Before correction

Because I will update it later, so I don't have to decide now. Later, when I checked that the error occurred because it was initialized properly ... ** "The Java compiler performs some parsing including control statements (if, switch, for, etc.) to determine compilation errors. In this program, the process of initializing the result is the switch statement. If it does not meet the conditional expression, it will not be executed, so it is judged as a compile error that "it may not have been initialized". ** Then it's okay if you initialize the field from the beginning, right? → By the way, if the value of the field is not updated, an error will be displayed.

** Resource leak never closed ** The most unclear point this time. As a result of investigation, it is said that the instance of the Scanner class should be closed. I learned Scanner while learning Progate, but I've never done that kind of processing before. Above all, I didn't understand it because it didn't cause a compile error. It seems that it was OK if I wrote sc.colse (); for the time being. Is it a rough interpretation that you should close it when you are done with it due to memory problems? ?? There is something wrong with the Japanese word "it will never be closed". Is it a mistake of "not closed"?

Issues and improvements

I feel like I can put together more. When I realized that I was supposed to use boolean type, this happened. Even if I enter an integer in the week field, if it is not correct, I tried to redo it → I gave up because I don't know what to do when a minus or a character is typed.

Comments, what you would like to know

** ・ Points to put together well ** I noticed during posting now that I could make 6 patterns depending on whether it was a time zone x a holiday. It should have been stored more beautifully ... I would like to know if you have the idea of doing this.

** ・ // How to use comments ** Anyway, I didn't have a chance to show the source code to others, so I don't know how to use the comments well. What else should I tell you? Is it just hard to see if you add too many comments?

** ・ What is a resource leak? ** ** I looked it up, but I don't think I can understand it very well. What kind of keyword should I search for?

in conclusion

I'm really just starting to learn, so I only know the basic fields From now on, I will publish my creations regularly, including what I was thinking I hope to make it public. I would be happy if you could give me a response.

Recommended Posts

[Java beginner] Resource leak <~~> will never be closed
java beginner 4
java beginner 3
java beginner
[Java beginner] Resource leak <~~> will never be closed
Java formatted output [Notes]
Save Java HTML as PDF
Java Stream cannot be reused.
JavaScript as seen from Java
It seems that data class-like functions will be added in Java14
Java Beginner Exercises
Java Exercise "Beginner"