- 공백이 아닌 『,』로 단락 구분 → useDeliniter("\\s*,\\s*")
import java.util.Scanner;
public class Test019
{
public static void main(String[] args)
{
// 주요 변수 선언
Scanner sc = new Scanner(System.in);
String name;
int kor, eng, mat, tot;
// 연산 및 처리
System.out.print("이름,국어,영어,수학 입력(『,』 구분) : ");
sc = new Scanner(sc.next()).useDelimiter("\\s*,\\s*");
// ---------
// "홍길동,90,80,70"
//
// new Scanner(sc.next()).useDelimiter("\\s*,\\s*");
// .구분자사용();
// "\\s*,\\s*"
// \s*,\s*
//
name = sc.next();
kor = sc.nextInt();
eng = sc.nextInt();
mat = sc.nextInt();
tot = kor + eng + mat;
System.out.println("\n>> 이름 : " + name);
System.out.println("\n>> 총점 : " + tot);
}
}
[Test019 실행 결과]
이름,국어,영어,수학 입력(『,』구분) : 홍길동,90,80,70
>> 이름 : 홍길동
>> 총점 : 240
계속하려면 아무 키나 누르십시오 . . .
- 공백이 아닌 『,』로 단락 구분 → useDeliniter("\\s*,\\s*")
import java.util.Scanner; public class Test019 { public static void main(String[] args) { // 주요 변수 선언 Scanner sc = new Scanner(System.in); String name; int kor, eng, mat, tot; // 연산 및 처리 System.out.print("이름,국어,영어,수학 입력(『,』 구분) : "); sc = new Scanner(sc.next()).useDelimiter("\\s*,\\s*"); // --------- // "홍길동,90,80,70" // // new Scanner(sc.next()).useDelimiter("\\s*,\\s*"); // .구분자사용(); // "\\s*,\\s*" // \s*,\s* // name = sc.next(); kor = sc.nextInt(); eng = sc.nextInt(); mat = sc.nextInt(); tot = kor + eng + mat; System.out.println("\n>> 이름 : " + name); System.out.println("\n>> 총점 : " + tot); } }
[Test019 실행 결과]
이름,국어,영어,수학 입력(『,』구분) : 홍길동,90,80,70 >> 이름 : 홍길동 >> 총점 : 240 계속하려면 아무 키나 누르십시오 . . .