✨
Java
  • 자바의 역사
  • Ch1
    • Before Start Java
      • History of Java
      • Feature of Java
      • API Documentation
      • Hello World!
      • eclipse shortcut
      • Eclipse IDE
      • GitHub to eclipse
  • Ch2
    • Variable
      • Variable
    • Variable Type
    • Character and String
    • Primitive type and range
    • printf and formatter
    • Scanner
    • Overflow of Int
    • Transition between different variable types
    • Object Array
  • CH3
    • Operator
  • CH4
    • 조건문과 반복문
    • if statement
    • switch
    • Math.random()
    • for statement
    • while statement
    • break, continue, named iterated statement
  • CH5
    • Array
    • 배열 활용
    • String Array
  • OOP
    • Intro
  • Class and Object
  • Make multiple Classes in one file
  • How to use Object
  • Object Array
  • Variable type according to declared location
  • Class Variable and Instance Variable
  • Method
  • Call Stack
  • Parameter
  • Static method(Class method) and Instance method
  • (Method)Overloading
  • Constructor
  • Constructor this()
  • Reference type variable "this"
  • Initialize variable
  • Inheritance
  • Composite
  • Single Inheritance, Object Class
  • (Method)Overriding
  • super, super()
  • package, class path
  • import, static import
  • modifier
  • access modifier
  • Encapsulation
  • Polymorphism
  • reference type transition
  • instanceof operator
  • Parameter polymorphism
  • Multiple object using one array
  • Abstract Class, Abstract Method
  • Creating Abstract Class
  • Interface
  • Interface Polymorphism
  • Interface Pros
  • Default method and static method
  • Inner Class
  • Anonymous Class
  • java.lang package and useful class
    • Object class
    • hashCode(), toString()
    • String class
    • StringBuffer class
    • StringBuilder class
    • Math class
    • Wrapper class
    • Number class
    • String to Number, String to Wrapper class
    • Auto-boxing and (auto)Unboxing
  • Collection Framework
    • Collections framework
    • List, Set, Map Interface
    • List의 removeAll() 과 clear() 비교
    • List Interface - ArrayList
    • How to view Java API source
    • List Interface - LinkedList
    • Stack and Queue
    • Iterator, ListIterator, Enumeration
    • Array
    • Comparator와 Comparable
    • Stack
    • String
    • String + char = String
    • String.toCharArray()
    • BufferedReader / BufferWriter
    • Scanner로 String 입력 - next( )와 nextLine( )
    • 공백없이 2차원배열 데이터 String으로 한번에 한줄씩 입력받기(문자/숫자)
    • 공백을 사이에 두고 빠른 2차원 배열 입출력
    • arr[index++]과 arr[index] index++의 차이
    • int와 long의 차이
    • Untitled
    • 타입 간의 변환
    • Array 와 ArrayList
    • valueOf()
    • Char
    • 변수, 객체 초기화(초기값 설정)
  • error troubleshooting
    • No enclosing instance of type
    • ASCII Code Table
    • java.lang.NumberFormatException
    • No enclosing instance..
  • reference
    • String을 생성하는 2가지 방법과 차이점
    • StackOverflowError(스택, 힙 메모리)
    • swtich-case 반복문
Powered by GitBook
On this page

Was this helpful?

  1. Collection Framework

Scanner로 String 입력 - next( )와 nextLine( )

next( )와 nextLine( ) 모두 Scanner로 String을 입력받을 때 사용하는 것이고, 리턴타입이 String이므로 숫자나 다른 형태로 입력받고 싶을 땐 형변환을 적절하게 해주어야한다! ex) int num = Integer.parseInt(line[1].charAt(0)) char cmd = line[1].charAt(0)

next( ) : 문자 또는 문자열을 공백을 기준으로 한 단어 또는 한문자씩 입력 받는다.

String s = sc.next();//s = Hello Java

System.out.println(s);//Hello 

nextLine( ) : 문자 또는 문장 한 라인 전체를 입력받는다. '\n' 을 포함하는 한 라인을 읽고 '\n'을 버린 나머지만 리턴한다.

String s = sc.nextLine();//s = Hello Java

System.out.println(s);//Hello Java

nextInt( )와 nextLine( ) 섞어 사용할 경우 주의 필요

import java.util.Scanner;
 
public class JavaScanner{
 
    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        String name;
        int age;
             
        System.out.println("나이를 입력하세요.");
        age = scan.nextInt();
        System.out.println("이름을 입력하세요.");
        name = scan.nextLine();
 
        System.out.printf("나이는 %d입니다.%n",age);
        System.out.printf("이름은 %s입니다.%n",name);

 scan.close();

    }
 
}

실행 결과 나이를 입력하세요 25 이름을 입력하세요. 나이는 25입니다. 이름은 입니다.

nextInt( )와 같이 타입을 지정해서 받는 메소드는 'Enter' 값을 무시하고 해당 타입만 받아 변환하여 반환하는데 이때 컴퓨터 내부에서는 'Enter' 값이 아직 남아있기 때문에 nextLine( )에서 'Enter' 값을 받아들이고 그대로 입력되어 종료된다. 그렇기 때문에 nextInt( )와 그 다음에 nextLine( )을 함께 사용할 경우 중간에 nextLine( )을 한번 더 사용해주거나 next( )를 사용한다.

명령어(String) 입력받을 때 - nextLine( ) 사용

  1. String str = sc.nextLine( );

  2. String[ ] line = str.split(" ");

  3. String cmd = line[0] 또는 char cmd = line[0].charAt(0)

또는 next( ) 사용

  1. String cmd = sc.next( )

PreviousBufferedReader / BufferWriterNext공백없이 2차원배열 데이터 String으로 한번에 한줄씩 입력받기(문자/숫자)

Last updated 4 years ago

Was this helpful?