Created
October 11, 2014 21:24
-
-
Save OlgaKulikova/babaa18c042c32ed5b6e to your computer and use it in GitHub Desktop.
Аналог Integer.parseInt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.Module2.Lesson1.Lesson3; | |
// Написать аналог Integer.parseInt. Использовать исключения. | |
import java.util.Scanner; | |
public class Task3 { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
String s = scan.nextLine(); | |
int n; | |
// Вариант первый | |
try { | |
n = new Integer(s); | |
System.out.println(n); | |
} catch (NumberFormatException e) { | |
System.out.println("Неверный формат строки!"); | |
} | |
// Вариант второй | |
try { | |
n = Integer.valueOf(s); | |
System.out.println(n); | |
} catch (NumberFormatException e) { | |
System.out.println("Неверный формат строки!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment