package HelloWorld.src.fc.java.part3;

import java.util.Scanner;

public class CarTest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("자동차 일련번호:");
        int carSn = scanner.nextInt();

        scanner.nextLine();

        System.out.print("자동차 이름:");
        String carName = scanner.nextLine();

        System.out.print("자동차 가격:");
        int carPrice = scanner.nextInt();

        scanner.nextLine();

        System.out.print("자동차 소유자:");
        String carOwner = scanner.nextLine();

        System.out.print("자동차 연식:");
        int carYear = scanner.nextInt();

        scanner.nextLine();

        System.out.print("자동차 타입:");
        String carType = scanner.nextLine();

        System.out.println(carSn + "\t" + carName + "\t" + carPrice + "\t" + carOwner + "\t" + carYear + "\t" + carType);

    }
}

 

 

이런식으로 중간에 scanner.nextLine();을 생략하면 마지막에 타입을 입력하지 못하고

바로 출력되는 경우가 생긴다.

그렇다고 모든 입력 끝에 scanner.nextLine();을 작성하진 않는다.

 

왜일가?

 

nextInt() 또는 nextDouble() 후에 nextLine()을 추가로 호출하는 이유는,

숫자 입력 후 남아 있는 줄바꿈 문자가 이후 nextLine()에서 처리되는 것을 방지하기 위해서이다.

'Java' 카테고리의 다른 글

생성자 매개변수  (1) 2025.02.13

 

package com.example.yaejunshin_finalproject;

public class Car {
    private int id;
    private String name;
    private int price;
    private String owner;
    private int year;

    public Car(int id, String name, int price, String owner, int year) {
        this.id = id;

        //차량 이름 검증(공백금지)
        if(name == null || name.isEmpty()){
            throw new IllegalArgumentException("차량 이름은 필수이며, 공백일 수 없습니다.");
        }
        this.name = name;

        //차량 가격 검증(0보다 커야 함)
        if(price <= 0){
            throw new IllegalArgumentException("차량 가격은 0 보다 커야 합니다.");
        }
        this.price = price;

        //차량 소유자 검증(null 또는 빈 문자열은 허용하지 않습니다.)
        if(owner == null || owner.isEmpty()){
            throw new IllegalArgumentException("차량 소유자는 필수이며, 공백일 수 없습니다.");
        }
        this.owner = owner;

        //차량 연식 검증(1950보다 커야함)
        if(year <= 1950){
            throw new IllegalArgumentException("차량 연식은 1950 보다 커야 합니다.");
        }
        this.year = year;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getPrice() {
        return price;
    }

    public String getOwner() {
        return owner;
    }

    public int getYear() {
        return year;
    }

    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price='" + price + '\'' +
                ", owner='" + owner + '\'' +
                ", year=" + year +
                '}';
    }
}

 

 

package com.example.yaejunshin_finalproject;

public class User {
    private int id;
    private String name;
    private String email;

    public User(int id, String name, String email) {
        this.id = id;

        //유저 이름 검증(공백금지)
        if(name == null || name.isEmpty()){
            throw new IllegalArgumentException("null 또는 빈 문자열은 허용하지 않습니다.");
        }
        this.name = name;

        //유저 이메일 검증(필수이며 공백금지)
        if(email == null || email.isEmpty()){
            throw new IllegalArgumentException("유저 이메일은 필수이며, 공백일 수 없습니다.");
        }
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }
}

 

 

package com.example.yaejunshin_finalproject;

import java.time.LocalDate;
import java.time.LocalTime;

public class Reservation {
    private int id;
    private String name;
    private User user;
    private Car car;
    private LocalDate date;
    private LocalTime time;

    public Reservation(int id, String name, User user, Car car, LocalDate date, LocalTime time) {
        this.id = id;
        this.name = name;
        this.user = user;
        this.car = car;
        this.date = date;
        this.time = time;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public User getUser() {
        return user;
    }

    public Car getCar() {
        return car;
    }

    public LocalDate getDate() {
        return date;
    }

    public LocalTime getTime() {
        return time;
    }

    @Override
    public String toString() {
        return "Reservation{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", user=" + user +
                ", car=" + car +
                ", date=" + date +
                ", time=" + time +
                '}';
    }
}

https://jdk.java.net/archive/

 

Archived OpenJDK GA Releases

Archived OpenJDK General-Availability Releases This page is an archive of previously released builds of the JDK licensed under the GNU General Public License, version 2, with Classpath Exception. WARNING: These older versions of the JDK are provided to he

jdk.java.net

 

개인의 컴퓨터에 맞게 버전을 설치해 줄 것인데,

필자는 11.0.2 윈도우 버전을 설치했다.

 

스프링으로 개발하기 위해 필요한 도구들

자바 개발 도구 : Java11

통합개발 환경 : STS, IntelliJ(Ultimate 유료버전만)

웹 서버 : Tomcat 9

웹 브라우저 : Chrome

데이터 베이스 : MySQL 5.7

기타 : VS code, Git, AWS, Maven

 

우선 STS를 설치할 것이다.

https://github.com/spring-attic/toolsuite-distribution/wiki/Spring-Tool-Suite-3

 

Spring Tool Suite 3

the distribution build for the Spring Tool Suite and the Groovy/Grails Tool Suite - spring-attic/toolsuite-distribution

github.com

 

 

IntelliJ 유료 설치(30일 무료)

https://www.jetbrains.com/ko-kr/idea/download/?section=windows

 

최고의 Java 및 Kotlin IDE인 IntelliJ IDEA를 다운로드하세요

 

www.jetbrains.com

 

Tomcat 9 다운로드

톰캣은 EE에 포합된다(Enterprise Edition)

https://tomcat.apache.org/download-90.cgi

 

Apache Tomcat® - Apache Tomcat 9 Software Downloads

Welcome to the Apache Tomcat® 9.x software download page. This page provides download links for obtaining the latest version of Tomcat 9.0.x software, as well as links to the archives of older releases. Unsure which version you need? Specification version

tomcat.apache.org

 

 

VisualStudio Code 설치

https://code.visualstudio.com/download

 

Download Visual Studio Code - Mac, Linux, Windows

Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows. Download Visual Studio Code to experience a redefined code editor, optimized for building and debugging modern web and cloud applications.

code.visualstudio.com

 

Git 다운로드

https://git-scm.com/downloads

 

Git - Downloads

Downloads macOS Windows Linux/Unix Older releases are available and the Git source repository is on GitHub. Latest source Release 2.47.0 Release Notes (2024-10-06) Download Source Code GUI Clients Git comes with built-in GUI tools (git-gui, gitk), but ther

git-scm.com

 

 

제목을 Test로 지정한 후 프로젝트를 생성

 

 

그러면 아래 그림과 같이 빈 프로젝트가 생성된다.

 

 

src에 우클릭을 한 후 보기와 같이 새로운 클래스를 만들어 준다.\

그리고 클래스의 이름은 ForBlog로 지정하였다.

 

그러면 보기와 같이 클래스가 만들어진다.

 

클래스를 쉽게 사람으로 생각하면 사람이라고 생각하면 된다.

사람이 무슨 동작을 하기 위해 행동이 필요하다.

이를 메서드라 부른다.

기본 클래스는 main이라는 메서드가 필요하다.

 

main을 입력한 후 tab or enter or 더블클릭을 하면 보기와 같이 기본 main클래스가 만들어진다.

자바에는 무언가 출력을 할때

System.out.println();

이런 기본형태가 필요하고 저 괄호안에 원하는 값을 입력하면 된다.

그리고 나서 초록색 버튼을 클릭하고 실행 시키고

아래 콘솔창을 보면 Hello 가 찍힘을 확인할 수 있다.

 

실행을 하고 나서 왼쪽 사이드바를 자세히 보면 새로운 폴더가 생성됨을 알 수 있다.

 

이것에 관하여는 학원의 관련자료로 설명하겠다.

우선 설명을 하자면 소스코드는 우리가 만든 부분이고,

그것을 실행하기 위해 API가 필요하며

실행을 하면 실행된 파일인 out에 class가 만들어진 것이다.

 

우리는 터미널을 열어서 컴파일을 초록색 화살표 버튼이 아닌 명령어로 실행해 볼 수 있다.

여기서 -encoding UTF-8은 만약에 수동으로 출력할때 한글이 끼어져 있으면 글씨가 깨지기 때문에

인코딩 형식을 직접 지정해주어야 한다.

 


 

중요 : JVM과 자바의 구동 방식

 

 

전에 유니티를 사용했을 때 Window로 작업을 하다 Mac으로 넘어갈 경우

라이더를 구매하지 않으면 한글이 깨지고 그런 현상이 있었는데,

자바는
1차적으로 src코드가 bytecode로 저장된다.
또한 실행을 하더라도 한번더 JVM을 거쳐서 exe로 마지막에 로딩하여 실행하기 때문에
다른 프로그램이랑 다르게 독립적으로 OS와 상관없이 작동한다.

 

 

내일배움카드

혜택으로 유니티를 수료하였고

이번에는 k-digital 기초역량훈련을 수강하게 되었다.

 

자바는 개발에 필요한 요소

1. JDK 설치

2. 자바 통합 개발에 필요한 도구인 IntelliJ or Eclipse (요새는 주로 IntelliJ를 사용)

 

자바 언어로 만들 수 있는 프로그램의 유형 (3가지)

1. 데스크톱 응용소프트웨어 : JavaSE 플랫폼을 구축해야 함.

2. 웹 기반 응용소프트웨어 : JavaEE 플랫폼을 구축해야 함.

3. 모바일 기반 응용소프트웨어 : JavaME 플랫폼을 구축해야 함.

 

 

이 중에서 필자는 JavaSE를 설치 할 것이다.


JDK설치

 

https://openjdk.org/

 

OpenJDK

Learn about the key active Projects in the Community including Amber (high-productivity language features), Loom (lightweight concurrency), Panama (foreign functions and foreign data), Valhalla (primitive types and specialized generics), and, of course, th

openjdk.org

 

우선 이 사이트로 접속 후

이 곳을 클릭

 

그 후 SE11클릭

 

윈도우를 사요할 것이므로 윈도우버전으로 설치하는데

이 부분에 관해서는 각각의 OS에 맞춰 설치하면 된다.

 

그 후 보기와 같은 순서로 경로를 지정해 줘야한다.

 

환경 변수 창에 들어오면 아래쪽의 시스템 변수의 새로 만들기를 누른 후

변수 이름 : JAVA_HOME

변수 값 : 앞서 설치한 JDK11의 위치를 찾아서 넣어주면 된다.

 

환경 변수 만들기가 끝났으면

시스템 변수에서 Path를 찾아서 더블 클릭후 새로만들기

%JAVA_HOME%\bin 추가

이때 / 가 아닌 역슬래시인 백스페이스키 아래의 \ 이니 주의

그 후 위로 이동을 눌러 최상단으로 위치시켜준다.

 

잘 설치가 되었는지 확인 하기 위해서는 윈도우 자체에 깔려있는

실행창에 cmd 검색후 들어가서

java -version 입력을 하면 버전을 확인 할 수 있다.

 


IntelliJ 설치

 

https://www.jetbrains.com/ko-kr/idea/download/?section=windows

 

최고의 Java 및 Kotlin IDE인 IntelliJ IDEA를 다운로드하세요

 

www.jetbrains.com

해당 사이트로 접속 후

Ultimate 버전은 학생인 경우 학교를 등록하지 않으면

1달 사용후 유료이므로 우선 Community 버전을 설치

※마찬가지로, 자신의 OS에 맞는 버전을 설치

 

IntelliJ설치를 할때 다음을 누르다

정보 보내기?가 있으면 Don't send

설치 경로는 기본(Default)로 진행하면 된다.

 

체크는 이렇게 하면되고, 이에 대한 설명이다.

 

 

그 후 절차대로 다음을 눌러가며 진행하면 설치가 완료된다.

 

IntelliJ를 설치하면 이런 화면이 나오는데

 

 

새 프로젝트를 클릭

 

위치는 임시로 C드라이브에 만들어놓은 곳을 지정하였고

왼쪽을 확인하면 언어는 자바로 설정

그리고 JDK를 눌러보면 여러가지가 있고 설치를 할 수 있는데

디스크에서 JDK 추가 -> 보기와 같이 클릭 -> 11버전이 JDK에 추가된다.

그리고 생성하면 완료

 

 


 

정보

1. 자바개발 4가지 플랫폼
=> JavaSE, JavaEE, JavaME, JavaFX

2. JavaSE개발환경을 구축하기 위해서 설치하는 프로그램
=> JDK(Java Development Kit)

3. JDK를 설치하면 PC에 설치 되는 것들
=> JVM, API, TOOL

4. 자바 프로그램을 구동하는 프로세서
=> JVM(Java Virtual Machine)

+ Recent posts