Skip to content

Instantly share code, notes, and snippets.

@OlgaKulikova
Created October 11, 2014 21:24
Show Gist options
  • Save OlgaKulikova/babaa18c042c32ed5b6e to your computer and use it in GitHub Desktop.
Save OlgaKulikova/babaa18c042c32ed5b6e to your computer and use it in GitHub Desktop.
Аналог Integer.parseInt
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