Backend/JAVA

JAVA 코딩 API (Application Programming Interface) - Math, System, Date, Scanner

쏠솔랄라 2023. 3. 3. 09:22

 

 

api.lang.Math

 

: 수학 계산 클래스

 

public class Test01 {

public static void main (String[] args) {

 

double a = 1.5;

 

System.out.println(Math.floor(a)); // 버림

System.out.println(Math.round(a)); // 반올림

System.out.println(Math.ceil(a)); // 올림

 

int b = 3;

int c = 4;

System.out.println(Math.pow(b, 3)); // 제곱근 구하기

System.out.println(Math.pow(c, 2)); // 제곱근 구하기

double d = Math.pow(b, 2) + Math.pow(c, 2);

double e = Math.sqrt(d);

System.out.println(e);

}

}

 

  


 

 

api.lang.System

 

: java.lang.System 운영체제와 관련한 정보를 가진 클래스

 

public class Test01 {

public static void main (String[] args) {

 

Test01 t = new Test01();

System.out.println(t);

 

System.err.println(t.getClass().getName() + "@" + Integer.toHexString(t.hashCode()));

 

// 시스템 정보 불러오기

System.out.println(System.getProperty("os.name"));

System.out.println(System.getProperty("java.runtime.version"));

System.out.println(System.getProperty("user.home"));

}

}

 

 


 

 

api.util.Date

 

: java.util.Date 시스템의 시간을 구하는 클래스

 

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test01 {
public static void main (String[] args) {

Date a = new Date();

System.out.println(a);
System.out.println(a.getMonth());
System.out.println(a.getDay());
System.out.println(a.getDate());

Format f = new SimpleDateFormat("yyyy년 MM월 dd일 hh시 mm분 ss초");
String time = f.format(a);
System.out.println(time);

 

// 지역설정

Locale.setDefault(Locale.CANADA);

System.out.println(Locale.CANADA);

 

Date d = new Date();

System.out.println(d);

 

 

//format : 형식

// simpleDateFormat 날짜 형식

// DecimalFormat 숫자 형식

 

int a = 123456789;

double b = 123.45;

 

Format f1 = new DecimalFormat("#,###.####"); // #숫자를 이 자리에 찍어라 (없으면 말고)

System.out.println(f1.format(a));

System.out.println(f1.format(b));

 

Format f2 = new DecimalFormat("0,0000.0000"); // 숫자를 이 자리에 찍어라(없어도 찍어라)

System.out.println(f2.format(a));

System.out.println(f2.format(b));

}
}

 

 


 

 

api.util.Scanner

 

import java.util.Scanner;

public class Test01 {

public static void main (String[] args) {

 

String song = "어둠만이 나의 전부였던 동안\r\n"

+ "숨이 벅차도록 달려왔잖아\r\n"

+ "Never say “time's up”\r\n"

+ "경계의 끝자락\r\n"

+ "내 끝은 아니니까\r\n"

+ "울타리 밖에 일렁이는 무언가\r\n"

+ "그 아무도 모르는 별일지 몰라\r\n"

+ "I wanna wanna be there\r\n"

+ "I'm gonna gonna be there\r\n"

+ "벅찬 맘으로 이 궤도를 벗어나\r\n"

 

Scanner sc = new Scanner(song);

// class 명과 패키지 명이 겹치는 경우에는 java.util.Scanner의 형태로 패키지명까지 전부 불러오면 된다

// ex. Class Scanner -> java.util.Scanner sc = new java.util.Scanner();

 

while (sc.hasNext()) {

System.out.println(sc.hasNext());

System.out.println(sc.next());

}

 

sc.close();

 

System.out.println("=======================================");

 

sc = new Scanner(song);

 

while (sc.hasNext()) {

System.out.println(sc.hasNext());

System.out.println(sc.next());

}

 

sc.close();

 

}

}

 

출력화면

 

import java.util.Scanner;

public class Test02 {

public static void main (String[] args) {

 

// .next는 공백문자(띄어쓰기)를 기준으로 단어를 구분한다

 

String text = "빨-주-노-초/파-남-보";

 

Scanner s1 = new Scanner (text);

 

while(s1.hasNext()) {

System.out.println(s1.next());

}

 

s1.close();

 

// - 를 기준으로 데이터 가져오기

 

Scanner s2 = new Scanner(text);

 

s2.useDelimiter("-"); // 구분문자(구분자)를 지정하는 메소드

s2.useDelimiter("/");

s2.useDelimiter("[-/]"); // '-'와 '/'로 구분하겠다

 

while(s2.hasNext()) {

System.out.println(s2.next());

}

s2.close();

}

}

 

출력화면

 

 

import java.io.IOException;

import java.net.URL;

import java.util.Scanner;

public class Test03 {

 

public static void main (String[] args) throws IOException {

 

// URL을 읽는 스캐너

 

URL url = new URL("https://www.naver.com/");

Scanner sc = new Scanner(url.openStream(), "UTF-8");

 

String text = "";

 

while (sc.hasNextLine()) {

text+=sc.nextLine() + "\r\n";

}

 

sc.close();

 

System.out.println(text);

}

}

 

 

출력화면