✨
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. error troubleshooting

java.lang.NumberFormatException

PreviousASCII Code TableNextNo enclosing instance..

Last updated 3 years ago

Was this helpful?

문자열을 수로 변환할 때 발생하는 에러이다.

내가 가장 많이 하는 입력 실수 유형

문자열에 숫자가 아닌 문자가 들어있는 경우에도 이 에러가 발생한다. 위의 코드 입력이 아래와 같다면 런타임 에러가 발생한다! 1 2도 아래의 코드와 같은 이유로 발생한다고 할 수 있다! 왜냐하면 공백 한 칸이 숫자가 아닌 문자이기 때문이다!!!!!!

1a2 233

위의 소스에서처럼 2개의 숫자를 각각 한줄씩 X2 두 줄에 걸쳐 입력받는데, 아래와 같은 경우도 에러가 발생한다!

123

정수 하나만 입력으로 들어간다면 8번째 줄의 2번째 숫자를 입력받는 br.readLine()은 null을 리턴하기 때문이다! 따라서 int b = Integer.parseInt(null); 과 같은 의미를 갖고, null은 Integer로 변환할 수 없기 때문에 런타임 에러가 발생한다!

parseInt 구현된 부분과 에러나는 이유 : 입력 Strign의 길이가 0보다 작기 때문에 에러가 리턴되는 것이다!

아래처럼 [4][8]인 데이터를 입력받고 싶다면 아래 코드처럼 구현하면 된다. 10101010 01010000 00001111 11110000

char[][] input = new char[4][8];
for(int i=0;i<4;i++) {
		input[i] = br.readLine().toCharArray();
}

근데 틀린 이유는 위의 코드에 앞서 br.readLine()이 하나 더 있었다. 그래서 배열 입력 한 줄은 하나씩 앞당겨져 가고 4번째(마지막) 입력은 결국 null을 리턴하고, parseInt(null)이 되어 계속 에러가 난 것이다.

https://java.lang.NumberFormatExceptionjava.lang.numberformatexception
parseInt 구현된 부분
parseInt() 에러나는 원인