java
이 튜토리얼에서는 예제를 통해 Java 스캐너와 그 방법에 대해 알아볼 것입니다.
Scanner java.util 클래스 패키지는 입력 스트림, 사용자, 파일 등과 같은 다양한 소스에서 입력 데이터를 읽는 데 사용됩니다. 예를 들어 보겠습니다.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// creates an object of Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
// takes input from the keyboard
String name = input.nextLine();
// prints the name
System.out.println("My name is " + name);
// closes the scanner
input.close();
}
} 출력
Enter your name: Kelvin My name is Kelvin
위의 예에서 줄을 확인하십시오.
Scanner input = new Scanner(System.in);
여기에서 Scanner의 개체를 만들었습니다. 명명된 입력 .
System.in 매개변수는 표준 입력에서 입력을 받는 데 사용됩니다. 키보드에서 입력을 받는 것처럼 작동합니다.
그런 다음 nextLine()을 사용했습니다. Scanner 메소드 사용자로부터 한 줄의 텍스트를 읽는 클래스입니다.
이제 Scanner에 대한 아이디어가 생겼습니다. , 이에 대해 자세히 살펴보겠습니다.
위의 예에서 볼 수 있듯이 java.util.Scanner를 가져와야 합니다. Scanner를 사용하기 전에 패키지 수업.
import java.util.Scanner; 패키지 가져오기에 대해 자세히 알아보려면 Java 패키지를 방문하십시오.
<시간>
패키지를 가져오면 다음은 Scanner을 만드는 방법입니다. 개체.
// read input from the input stream
Scanner sc1 = new Scanner(InputStream input);
// read input from files
Scanner sc2 = new Scanner(File file);
// read input from a string
Scanner sc3 = new Scanner(String str);
여기에서 Scanner의 개체를 만들었습니다. InputStream, File 및 String에서 각각 입력을 읽을 클래스입니다.
Scanner 클래스는 다양한 유형의 입력을 읽을 수 있는 다양한 메서드를 제공합니다.
| 메소드 | 설명 |
|---|---|
nextInt() | int를 읽습니다. 사용자의 가치 |
nextFloat() | float를 읽습니다. 사용자의 가치 |
nextBoolean() | boolean를 읽습니다. 사용자의 가치 |
nextLine() | 사용자로부터 한 줄의 텍스트를 읽습니다. |
next() | 사용자의 단어 읽기 |
nextByte() | byte를 읽습니다. 사용자의 가치 |
nextDouble() | doubl를 읽습니다. 사용자의 e 값 |
nextShort() | short을 읽습니다. 사용자의 가치 |
nextLong() | long를 읽습니다. 사용자의 가치 |
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// creates a Scanner object
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer: ");
// reads an int value
int data1 = input.nextInt();
System.out.println("Using nextInt(): " + data1);
input.close();
}
} 출력
Enter an integer: 22 Using nextInt(): 22
위의 예에서는 nextInt()를 사용했습니다. 정수 값을 읽는 방법입니다.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// creates an object of Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter Double value: ");
// reads the double value
double value = input.nextDouble();
System.out.println("Using nextDouble(): " + value);
input.close();
}
} 출력
Enter Double value: 33.33 Using nextDouble(): 33.33
위의 예에서는 nextDouble()을 사용했습니다. 부동 소수점 값을 읽는 방법입니다.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// creates an object of Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
// reads the entire word
String value = input.next();
System.out.println("Using next(): " + value);
input.close();
}
}
출력
Enter your name: Jonny Walker Using next(): Jonny
위의 예에서는 next()을 사용했습니다. 사용자로부터 문자열을 읽는 메소드입니다.
여기에 전체 이름을 제공했습니다. 그러나 next() 메서드는 이름만 읽습니다.
next() 때문입니다. 메소드는 공백까지 입력을 읽습니다. 캐릭터. 공백 발견되면 문자열을 반환합니다(공백 제외).
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// creates an object of Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
// reads the entire line
String value = input.nextLine();
System.out.println("Using nextLine(): " + value);
input.close();
}
} 출력
Enter your name: Jonny Walker Using nextLine(): Jonny Walker
첫 번째 예에서는 nextLine()을 사용했습니다. 사용자로부터 문자열을 읽는 메소드입니다.
next()과 달리 , nextLine() 메소드는 공백을 포함한 전체 입력 라인을 읽습니다. 메소드는 다음 줄 문자 \n를 만나면 종료됩니다. .
권장 자료: nextLine()을 건너뛰는 자바 스캐너.
<시간>Java 스캐너는 큰 정수와 큰 십진수를 읽는 데에도 사용할 수 있습니다.
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// creates an object of Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter a big integer: ");
// reads the big integer
BigInteger value1 = input.nextBigInteger();
System.out.println("Using nextBigInteger(): " + value1);
System.out.print("Enter a big decimal: ");
// reads the big decimal
BigDecimal value2 = input.nextBigDecimal();
System.out.println("Using nextBigDecimal(): " + value2);
input.close();
}
} 출력
Enter a big integer: 987654321 Using nextBigInteger(): 987654321 Enter a big decimal: 9.55555 Using nextBigDecimal(): 9.55555
위의 예에서는 java.math.BigInteger을 사용했습니다. 및 java.math.BigDecimal BigInteger을 읽을 패키지 및 BigDecimal 각각.
Scanner 클래스는 전체 줄을 읽고 줄을 토큰으로 나눕니다. 토큰은 Java 컴파일러에 의미가 있는 작은 요소입니다. 예를 들어,
입력 문자열이 있다고 가정합니다.
He is 22 이 경우 스캐너 개체는 전체 줄을 읽고 문자열을 토큰으로 나눕니다. "He ", "이다 " 및 "22 ". 그런 다음 개체는 각 토큰을 반복하고 다른 방법을 사용하여 각 토큰을 읽습니다.
참고 :기본적으로 공백은 토큰을 나누는 데 사용됩니다.
java
자바 PrintStream 클래스 이 자습서에서는 예제를 통해 Java PrintStream 클래스와 해당 print() 및 printf() 메서드에 대해 배웁니다. PrintStream java.io 클래스 패키지는 바이트 대신 일반적으로 읽을 수 있는 형식(텍스트)으로 출력 데이터를 쓰는 데 사용할 수 있습니다. 추상 클래스 OutputStream를 확장합니다. . PrintStream 작업 다른 출력 스트림과 달리 PrintStream 기본 데이터(정수, 문자)를 바이트 대신 텍스트 형식으로 변환합니다. 그런 다음
자바 InputStreamReader 클래스 이 자습서에서는 예제를 통해 Java InputStreamReader 및 해당 메서드에 대해 알아봅니다. InputStreamReader java.io 클래스 패키지를 사용하여 바이트 데이터를 문자 데이터로 변환할 수 있습니다. 추상 클래스 Reader을 확장합니다. . InputStreamReader 클래스는 다른 입력 스트림과 함께 작동합니다. 바이트 스트림과 문자 스트림 간의 브리지라고도 합니다. InputStreamReader 때문입니다. 입력 스트림에서 바이트를 문자로